Python - prettyprint JSON with json.dumps


Python tip:

You can use the indent parameter with json.dumps to get an indented multiline string instead of a single line.

It's much easier to read.

An example👇

import json

users = [
    {"username": "johndoe", "active": True},
    {"username": "Mary", "active": False}
]


print(json.dumps(users))
# [{"username": "johndoe", "active": true}, {"username": "Mary", "active": false}]


print(json.dumps(users, indent=2))
"""
[
  {
    "username": "johndoe",
    "active": true
  },
  {
    "username": "Mary",
    "active": false
  }
]
"""