Don't inject attribute with JQ if there was none

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?

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(.))'

Demo

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).