0

I’m frequently loading and dumping JSON data to and from maya , which is basically append a new dict of variables to a list . All working fine , but I’d really like the JSON to be more readable when debugging . Is there a way to format it better when dumping ? Currently it’s a single line item for my list

Eg

{“master”:[{dict},{dict}]}

The dict entries contain data structures . So it gets pretty long

Is there a way when dumping to represent the list on separate lines, formatting based off commas , a keyword , or something like that ?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
blam
  • 87
  • 1
  • 8
  • 2
    print(json.dumps(your_dict, sort_keys=True,indent=4, separators=(',', ': '))) – camp0 Aug 01 '19 at 07:48
  • @camp0 that’s perfect ! Literally what I needed . Can I use dumps to write this to the JSON file as well, or would that cause loading problems ( for example do I need to format the load if I wrote formatted data ) , hope my question makes sense – blam Aug 01 '19 at 07:59

1 Answers1

1

pandas library provides an excellent solution for this.

from pandas.io.json import json_normalize
normalized = json_normalize(your_json_file)

When you use the function once, it normalizes the first layer of json object. If your json object gets more complicated you can use this method as many times as necessary. Make sure you check out the documentation below;

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.json.json_normalize.html

Edit: If you wish to use it for debugging, you should checkout pprint library also .

import pprint

pprint.pprint(your_json_file)

simple as that.

Erdal Dogan
  • 557
  • 1
  • 4
  • 10