Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active June 14, 2020 07:26
Show Gist options
  • Save lamchau/3f9fafdd96f5ca569451539c1d2f93b9 to your computer and use it in GitHub Desktop.
Save lamchau/3f9fafdd96f5ca569451539c1d2f93b9 to your computer and use it in GitHub Desktop.
removing null/empty values from JSON
# from: https://github.com/stedolan/jq/issues/104
# more explicit (per value)
walk(
if type == "object" then
with_entries(
select(
.value != null and
.value != "" and
.value != [] and
.value != {}
)
)
else .
end
)
# faster (marginally)
delpaths([path(..?) as $p | select(getpath($p) == null or getpath($p) == [] or getpath($p) == {}) | $p])
# usage
jq 'delpaths([path(..?) as $p | select(getpath($p) == null or getpath($p) == [] or getpath($p) == {}) | $p])' <filename.txt>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment