1

I would like to improve / customize json dumps to improve clarity of outputs.

When I run:

import json

d = {
       'a': 1,
       'b': 2,
       'c': {
               'x': 9,
               'y': 8,
               'z': 7
            },
       'd': 3
    }

print(json.dumps(d, indent = 2))

The output is:

{
  "a": 1,
  "b": 2,
  "c": {
    "x": 9,
    "y": 8,
    "z": 7
  },
  "d": 3
}

I would like the output to instead be:

{
  "a": 1,
  "b": 2,
  "c": {
         "x": 9,
         "y": 8,
         "z": 7
       },
  "d": 3
}

I know it seems like a small change, but especially for larger dumps with multiple nested dictionaries, I believe that this will greatly improve clarity of outputs.

I have read a bit about custom JSON encoders such as what is suggested here: JSON dumps custom formatting but I have been unable to find anything I can customize for my application.

Any and all assistance is greatly appreciated.

perry_the_python
  • 385
  • 5
  • 14

0 Answers0