Python json.tool module

Python documentation here

Example usage:

$ echo '{"json":"obj"}' | python -mjson.tool
{
    "json": "obj"
}
$ echo '{ 1.2:3.4}' | python -mjson.tool
Expecting property name: line 1 column 2 (char 2)
   

All argument patterns are:

  • piped json string | python -m json.tool which syntax checks then outputs the results to stdout
  • python -m json.tool input_file.json which reads the path relative file and outputs the results
  • python -m json.tool input_file.json output_file.json only difference here is that the output is directed to the specified file

My thoughts, this could be part of some sort of data validation check, looking for corrupted json static files.

Example

$ echo '{"a":123, "foo":"bar" }' | python -m json.tool && echo "IS valid" || echo "Is not valid"
{
    "a": 123,
    "foo": "bar"
}
IS valid
$ echo '{"a"1:123, "foo":"bar" }' | python -m json.tool && echo "IS valid" || echo "Is not valid"
Expecting : delimiter: line 1 column 4 (char 4)
Is not valid

One thought on “Python json.tool module

  1. Pingback: Python – batteries included - Refactored scope

Comments are closed.