Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Atom1cByte/3301291a94cb3f6acbe44401c3d51a7c to your computer and use it in GitHub Desktop.

Select an option

Save Atom1cByte/3301291a94cb3f6acbe44401c3d51a7c to your computer and use it in GitHub Desktop.
quick someliner to truncate json with jq
jq 'map(
if type == "array" then
"[" + (.[0:5] | map(tostring) | join(", ")) + (if length > 5 then ", ..." else "" end) + "] (array, len=" + (length|tostring) + ")"
elif type == "object" then
"{ " + (to_entries[0:5] | map("\(.key): \(.value|tostring)") | join(", ")) + (if (to_entries | length) > 5 then ", ..." else "" end) + " } (object, keys=" + (keys|length|tostring) + ")"
else
(. | tostring) + " (" + (type) + ")"
end
)' file.json
@Atom1cByte
Copy link
Author

Atom1cByte commented Jun 12, 2025

better one:

jq 'to_entries | map(
  {
    key,
    value: (
      if (.value | type) == "array" then
        "[" + ((.value[0:5]) | map(tostring) | join(", ")) +
        (if (.value | length) > 5 then ", ..." else "" end) +
        "] (array, len=" + ((.value | length)|tostring) + ")"
      elif (.value | type) == "object" then
        "{ " + ((.value | to_entries)[0:5] | map("\(.key): \(.value|tostring)") | join(", ")) +
        (if (.value | to_entries | length) > 5 then ", ..." else "" end) +
        " } (object, keys=" + ((.value | keys | length)|tostring) + ")"
      else
        (.value | tostring) + " (" + (.value | type) + ")"
      end
    )
  }
) | from_entries' file.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment