Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading