DEV Community

Anurag Rana
Anurag Rana

Posted on

Pretty print JSON using the command line tools

Option 1. Using python
cat FileName.json | python -m json.tool

Option 2. Colors and multiple options.
cat FileName.json~ | jq ''

Option 3. Pretty print with syntax highlighting.
echo '{"foo": "bar"}' | python -m json.tool | pygmentize -g

Top comments (5)

Collapse
 
andypiper profile image
Andy Piper

Note, option 2 is fine, but no good if you are dealing with long integers (more than 53 bit) as JavaScript mangles the numbers.

Collapse
 
tbutterwith profile image
Tom Butterwith

Could you elaborate a little further? jq is built in C

Collapse
 
andypiper profile image
Andy Piper

Sure. Let's assume the following small JSON:

{
  "id": 1130950371172638721,
  "id_str": "1130950371172638721"
}

(hint, these are values from a Tweet object in the Twitter API)

Now, let's parse this with jq:

$ jq < simple.json
{
  "id": 1130950371172638700,
  "id_str": "1130950371172638721"
}

The large integer value is mangled, because JavaScript (jQuery) does not like values larger than 53 bits. This is the reason that Twitter ended up serving IDs as both integer values and as strings (read more here)

Thread Thread
 
tbutterwith profile image
Tom Butterwith

Well every day is a learning day. I didn't know C had the same issues as JS with integer parsing.

Thanks for clarifying.

Thread Thread
 
andypiper profile image
Andy Piper

I guess this issue is relevant here:

github.com/stedolan/jq/issues/369