How to Prettyprint a JSON File in Python


Using json module

The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by:


# Import module
import json

j = '[{"name":"foo", "age":10}]'

parsed = json.loads(j)
pretty = json.dumps(parsed, indent=2, sort_keys=True)
    "age": 10,
    "name": "foo"

Using Command Line

You can do this on the command line:

pretty printing
python3 -m json.tool some.json
echo '[{"name":"foo", "age":10}]' | python3 -m json.tool
        "name": "foo",
        "age": 10

Related Tags