0

I generate a json, such as the following.

{"discs": [{
    "label": "--", 
    "performer": "--", 
    "release": "--", 
    "title": "--",
    "performances": [{
                    "movements": [{
                            "duration": 293, 
                            "durationR": "4:53",
                            "...": ".."
                       }]
                 }]
}]
}

I want to format this json file as readable as the above, that is the simple properties are before more complicated content, but the real output is not controlled that.

My code is simple as

json.dump(data, outfile, indent=4, sort_keys=True)

Is there any methods to get that?

BollMose
  • 3,002
  • 4
  • 32
  • 41
  • 1
    To clarify, elements in the dictionaries should be ordered by how many sub-elements they contain? Elements with the least sub-elements should appear first? – SCB Dec 21 '17 at 03:25
  • https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file – Adarsh Dec 21 '17 at 03:27
  • @Adarsh Code already has done that – OneCricketeer Dec 21 '17 at 03:28
  • @cricket_007 I thought he was looking for other alternatives – Adarsh Dec 21 '17 at 03:30
  • Disagree @cricket_007. JSON is designed to be, to some extent, a human readable serialization format. It definitely is more so than something like pickle. Is there a more human readable alternative that you would suggest that meet's OPs requirements? – SCB Dec 21 '17 at 03:30
  • 1
    Use `yaml` if you are looking for readability. It even allows ***comments***!!! – Kaushik NP Dec 21 '17 at 03:41
  • @SCB JSON can be read easily, yes, but if it's just used to pass data around between programs, there's no reason for it to be pretty printed. That was my main point. – OneCricketeer Dec 21 '17 at 03:46
  • 2
    Possible duplicate of [How to custom-sort a list of dict to use in json.dumps](https://stackoverflow.com/questions/18871217/how-to-custom-sort-a-list-of-dict-to-use-in-json-dumps) – Galen Dec 21 '17 at 04:07

0 Answers0