Pretty print JSON dumps

Question:

I use this code to pretty print a dict into JSON:

import json
d = {'a': 'blah', 'b': 'foo', 'c': [1,2,3]}
print json.dumps(d, indent = 2, separators=(',', ': '))

Output:

{
  "a": "blah",
  "c": [
    1,
    2,
    3
  ],
  "b": "foo"
}

This is a little bit too much (newline for each list element!).

Which syntax should I use to have this:

{
  "a": "blah",
  "c": [1, 2, 3],
  "b": "foo"
}

instead?

Asked By: Basj

||

Answers:

Perhaps not quite as efficient, but consider a simpler case (somewhat tested in Python 3, but probably would work in Python 2 also):

def dictJSONdumps( obj, levels, indentlevels = 0 ):
    import json
    if isinstance( obj, dict ):
        res = []
        for ix in sorted( obj, key=lambda x: str( x )):
            temp = ' ' * indentlevels + json.dumps( ix, ensure_ascii=False ) + ': '
            if levels:
                temp += dictJSONdumps( obj[ ix ], levels-1, indentlevels+1 )
            else:
                temp += json.dumps( obj[ ix ], ensure_ascii=False )
            res.append( temp )
        return '{n' + ',n'.join( res ) + 'n}'
    else:
        return json.dumps( obj, ensure_ascii=False )

This might give you some ideas, short of writing your own serializer completely. I used my own favorite indent technique, and hard-coded ensure_ascii, but you could add parameters and pass them along, or hard-code your own, etc.

Answered By: Victoria

Another alternative is print(json.dumps(d, indent=None, separators=(',n', ': ')))

The output will be:

{"a": "blah",
"c": [1,
2,
3],
"b": "foo"}

Note that though the official docs at https://docs.python.org/2.7/library/json.html#basic-usage say the default args are separators=None –that actually means “use default of separators=(', ',': ') ). Note also that the comma separator doesn’t distinguish between k/v pairs and list elements.

Answered By: MarkHu

This has been bugging me for a while as well, I found a 1 liner I’m almost happy with:

print json.dumps(eval(str(d).replace('[', '"[').replace(']', ']"').replace('(', '"(').replace(')', ')"')), indent=2).replace('"\"[', '[').replace(']\""', ']').replace('"\"(', '(').replace(')\""', ')')

That essentially convert all lists or tuples to a string, then uses json.dumps with indent to format the dict. Then you just need to remove the quotes and your done!

Note: I convert the dict to string to easily convert all lists/tuples no matter how nested the dict is.

PS. I hope the Python Police won’t come after me for using eval… (use with care)

Answered By: Tickon

I ended up using jsbeautifier:

import jsbeautifier
opts = jsbeautifier.default_options()
opts.indent_size = 2
jsbeautifier.beautify(json.dumps(d), opts)

Output:

{
  "a": "blah",
  "c": [1, 2, 3],
  "b": "foo"
}
Answered By: Allen Z.

After years, I found a solution with the built-in pprint module:

import pprint
d = {'a': 'blah', 'b': 'foo', 'c': [1,2,3]}
pprint.pprint(d)                    # default width=80 so this will be printed in a single line
pprint.pprint(d, width=20)          # here it will be wrapped exactly as expected

Output:

{'a': 'blah',  
 'b': 'foo',  
 'c': [1, 2, 3]}

Answered By: Basj

I couldn’t get jsbeautifier to do much, so I used regular expressions. Had json pattern like

'{n    "string": [n        4,n        1.0,n        6,n        1.0,n        8,n        1.0,n        9,n        1.0n    ],n...'

that I wanted as

'{n    "string": [ 4, 1.0, 6, 1.0, 8, 1.0, 9, 1.0],n'

so

t = json.dumps(apriori, indent=4)
t = re.sub('[n {7}', '[', t)
t = re.sub('(?<!]),n {7}', ',', t)
t = re.sub('n {4}]', ']', t)
outfile.write(t)

So instead of one "dump(apriori, t, indent=4)", I had those 5 lines.

Answered By: user15118385