Assuming you have a valid JSON file containing a set of objects, like
{"a":123,"b":"sd","c":"x45","d":1,"e":"''"}
{"a":5,"b":"bfgg","c":"x4c","d":31,"e":"''"}
or the equivalent
{
"a": 123,
"b": "sd",
"c": "x45",
"d": 1,
"e": "''"
}
{
"a": 5,
"b": "bfgg",
"c": "x4c",
"d": 31,
"e": "''"
}
and you'd like to remove the d
and e
keys from each object.
Using jq
, deleting one key at a time:
jq -c 'del(.d) | del(.e)' file.json
Deleting both keys at once:
jq -c 'del(.d, .e)' file.json
The result of either of these would be
{"a":123,"b":"sd","c":"x45"}
{"a":5,"b":"bfgg","c":"x4c"}
A third way of doing this, which doesn't mention the actual keys by name, would be to convert the objects to lists of "entries" using to_entries
, then delete the last two entries and convert the list back to a modified object:
jq -c 'to_entries | del(.[-2:]) | from_entries' file.json
This is most like what the text of the question proposes and the result depends on the ordering of the keys in the object.