Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a field in nested objects in JQ

Tags:

json

key

jq

I have a JSON lines file with summary fields and other fields. I would like to delete all of the summary fields using JQ. This is my input:

{"object1": {"summary":"Some summary I want removing", "keepMe":"please"}}
{"object2": {"summary":"Delete me too!", "keepMe":"pretty please"}}

and this is the desired output:

{"object1": {"keepMe":"please"}}
{"object2": {"keepMe":"pretty please"}}
like image 927
Iain Duncan Avatar asked Mar 03 '23 14:03

Iain Duncan


1 Answers

There are several possible interpretations of the Q.

A simple approach that will solve the simplest interpretation would be to use:

map_values(del(.summary))

If you want to eliminate the "summary" field wherever it occurs, no matter how deeply nested, then I'd use walk/1, e.g.

walk(if type == "object" then del(.summary) else . end)
like image 135
peak Avatar answered Mar 15 '23 12:03

peak