How do I pretty print JSON in Python?

To pretty print a messy JSON string, you can use the json.dumps(indent) method of the built-in Python package named json. First, you need to use the json.loads() method to convert the JSON string into a Python object. Then you need to use json.dumps() to convert the Python object back to JSON. The json.dumps() method takes a number of parameters, including the "indent" level for the JSON arrays, which will be used to pretty-print the JSON data string. If the json.dumps() indentation parameter is negative 0, or an empty string, then there is no indentation, and only newlines are inserted. By default, the data is presented as a single-line JSON string with no indentation. In this Python Pretty Print JSON example, we load a minified JSON string and pretty print it using json.dumps() method. Click Execute to run the Python Pretty Print JSON example online and see result.
Loading...