-1

I store JSON data as a string (comming from json.dumps()) to files. But complexe data is not readable for humans because linebreaks and indents are missing.

>>> import json
>>> d = {'one': 1, 'group': [4,9,7]}
>>> json.dumps(d)
'{"one": 1, "group": [4, 9, 7]}'

But the string should look more like this.

{'one': 1,
 'group': [
           4,
           9,
           7
          ]
}

Can I realize this?

buhtz
  • 10,774
  • 18
  • 76
  • 149

1 Answers1

1

The json module in python will honor indenting if you pass an indent parameter:

import json

d = {'one': 1, 'group': [4,9,7]}
print json.dumps(d, indent=4, sort_keys=True)

will output:

   {
        "one": 1,
            "group": [ 
            4, 
            9, 
            7
        ]
    }
Bricky
  • 2,572
  • 14
  • 30