I have the following datastucture:
{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
When I use pprint in python, I am getting
{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
However, I would very much like it to be printed like:
{'row_errors': 
    {'hello.com': 
        {'template': [u'This field is required.']}}}
Is this possible to configure via pprint? (I prefer pprint because I am printing this inside a jinja template).
Basically the same way you would flatten a nested list, you just have to do the extra work for iterating the dict by key/value, creating new keys for your new dictionary and creating the dictionary at final step. For Python >= 3.3, change the import to from collections.
Deleting dictionaries from a Nested Dictionary Deletion of dictionaries from a nested dictionary can be done either by using del keyword or by using pop() function.
Like the wim suggested on comments, you can use json.dump.
Quoting from Blender's answer, you can achieve this like this:
The json module already implements some basic pretty printing with the indent parameter:
>>> import json
>>>
>>> your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print json.dumps(parsed, indent=4, sort_keys=True)
[
    "foo", 
    {
        "bar": [
            "baz", 
            null, 
            1.0, 
            2
        ]
    }
]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With