PythonFormat JSON
Format the Result
The example above prints a JSON string, but it is not very easy to read, with no indentations and line breaks.
Thejson.dumps()
method has parameters to make it easier to read the result:
Example
Use theindent
parameter to define the numbers of indents:
json.dumps(x, indent=4)
Try it Yourself »You can also define the separators, default value is (", ", ": "), which means using a comma and a space to separate each object, and a colon and a space to separate keys from values:
Example
Use theseparators
parameter to change the default separator:
json.dumps(x, indent=4, separators=(". ", " = "))
Try it Yourself »