+2 votes
in Programming Languages by (73.2k points)
How can I print json data with some indentation?

e.g.

{"Name": "ABC", "ID": 123}

as

{

    "Name": "ABC",

    "ID": 123

}

1 Answer

+1 vote
by (346k points)
selected by
 
Best answer

For the pretty printing of JSON data, you can use parameter "indent" with function dumps(). Here is an example:

>>> import json
>>> a = {'Name': 'ABC', 'ID': 123}
>>> v = json.dumps(a, indent=4)
>>> print(v)
{
    "Name": "ABC",
    "ID": 123
}

Related questions

+4 votes
1 answer
+3 votes
1 answer

...