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?
>Solution :
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, change select(.) to explicit select(. != null).