Skip to content

Instantly share code, notes, and snippets.

@SchumacherFM
Last active December 4, 2023 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SchumacherFM/dd522df72c75e461dff0ca7e9725d2e5 to your computer and use it in GitHub Desktop.
Save SchumacherFM/dd522df72c75e461dff0ca7e9725d2e5 to your computer and use it in GitHub Desktop.
Remove all empty values from a JSON file with CLI program jq
# removes all null, false, "", 0, [], {} and "0001-01-01T00:00:00Z values from a json
# files and writes the data back to the original file via program sponge (brew install moreutils jq)
# date time string because of GoLang empty time encoding 🙈
jsonRemoveEmpty() {
for file in *.json; do
cat "$file" | jq 'walk( if type == "object" then with_entries(select(.value != null and .value != false and .value != "" and .value != 0 and .value != [] and .value != {} and .value != "0001-01-01T00:00:00Z") ) else . end)' | sponge "$file"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment