Hacker News new | past | comments | ask | show | jobs | submit login

Python formats it almost exactly like you did:

  $ python3 -m json.tool
  {
    "squadName": "Super hero squad",
    "homeTown": "Metro City",
    "formed": 2016,
    "secretBase": "Super tower",
    "active": true,
    "members": [
        {
            "name": "Molecule Man",
            "age": 29,
            "secretIdentity": "Dan Jukes",
            "powers": [
                "Radiation resistance",
                "Turning tiny",
                "Radiation blast"
            ]
        },
        {
            "name": "Madame Uppercut",
            "age": 39,
            "secretIdentity": "Jane Wilson",
            "powers": [
                "Million tonne punch",
                "Damage resistance",
                "Superhuman reflexes"
            ]
        },
        {
            "name": "Eternal Flame",
            "age": 1000000,
            "secretIdentity": "Unknown",
            "powers": [
                "Immortality",
                "Heat Immunity",
                "Inferno",
                "Teleportation",
                "Interdimensional travel"
            ]
        }
    ]
  }



I don't python, but this scheme missed the point. Keys are vertically aligned, but values are not. Some brackets are vertically aligned, other are not (you have to hunt for the starting square brackets). Bracket take up a whole line each wasting vertical space.


I see, thanks. For me, the point is just "indent successive objects" so it suffices - especially when I'm in a Python mindset and mentally filtering out brackets anyway. But I could understand why you prefer the formatting as you do.


I think having the values aligned make it much quicker to read off entries. I will generally looked at the keys for the first entry, but for subsequent entries I assume the same ordering and just look at the values on the right.

It's a bit trivial, but another subtle effect that I like is that the sub-blocks are not mutually vertically aligned due to being offset by the semi-random length of the key

So for instance

    {"squadName":  "Super hero squad",
     "homeTown":   "Metro City",
     "formed":     2016,
     "secretBase": "Super tower",
     "active":     true,
     "members":    [{"name":           "Molecule Man",
                     "age":            29,
                     "secretIdentity": "Dan Jukes",
                     "powers":         ["Radiation resistance",
                                        "Turning tiny",
                                        "Radiation blast"]},
                    {"name":           "Madame Uppercut",
                     "age":            39,
                     "secretIdentity": "Jane Wilson",
                     "powers":         ["Million tonne punch",
                                        "Damage resistance",
                                        "Superhuman reflexes"]}],
    "enemies": [{"name":           "Eternal Flame",
                 "age":            1000000,
                 "secretIdentity": "Unknown",
                 "powers":         ["Immortality",
                                    "Heat Immunity",
                                    "Inferno",
                                    "Teleportation",
                                    "Interdimensional travel"]}]}
at a glance you see that the third person is not part of the same list as the first two

If all second level blocks are at the same level then you need to visually catch that there is extra key.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: