cat "$FileName" | jq --sort-keys 'map({"author_id": .author.id,"author": .author.name, "badge": .author.badges[0].title, "message", "timestamp", "time_in_seconds", "time_text"})' > "$TargetName"
produces output with "time_in_seconds": null if there was no "time_in_seconds" in source JSON. How to eliminate this:
- for this very attribute?
- for all attributes?
CodePudding user response:
To remove one field if it is null, use .time_in_seconds |= select(.).
To remove all fields that are null, use .[] |= select(.).
Add this inside your map at the end, like so:
jq 'map({…} | .[] |= select(.))'
Note: This will also delete fields with value false. If you want to restrict to null, make select(.) more explicit and change it to select(. != null).
CodePudding user response:
To remove all null values (or any arbitrary condition) in your input, you could make use of recurse/0 (..) and del/1.
del(.. | select(. == null))
