Today, I Learned...

by Justin Campbell

< All

Validate/format JSON by piping to python -m json.tool:

$ echo '{ "test": true }' | python -m json.tool
{
    "test": true
}
$ echo '{ "test": true? }' | python -m json.tool
Expecting , delimiter: line 1 column 14 (char 14)

It's part of the Python Standard Library, so it's already on your Mac and most Linux machines.

via @aaronfeng

@nlsmith also tells me that yajl supports something similar with json_reformat:

$ echo '{ "test": true }' | json_reformat 
{
    "test": true
}

$ echo '{ "test": true? }' | json_reformat 
lexical error: invalid char in json text.
                                        { "test": true? } 
                     (right here) ------^

There's also a json_verify command that does the same thing, minus spitting out pretty formatting. brew install yajl to get it.

October 3rd, 2012