Hacker News new | ask | show | jobs
by tchaffee 3171 days ago
JSON is a lot easier to read.

    {
      "employees": [
        {
          "name": "John Crichton",
          "gender": "male"
        },
        {
          "name": "Aeryn Sun",
          "gender": "female"
        }
      ]
    }
versus

    <employees>
      <employee>
        <name>John Crichton</name>
        <gender>male</gender>
      </employee>
      <employee>
        <name>Aeryn Sun</name>
        <gender>female</gender>
      </employee>
    </employees>
4 comments

It's a lot easier to read when a data format is written in JSON, when a document format is written in JSON the reverse is the general order (minus incidents where the format is obnoxiously constructed so as to defy understanding by outside parties)
I hate those comma's, forgetting them or having too many. So therefore I use YAML. Still way easier to read.

    employees:
        - name: John Crichton
          gender: male
        - name: Aeryn Sun
          gender: female
I like YAML and do find it slightly easier to read than JSON. But it hasn't won the popularity contest (yet), for whatever reasons.
Or

    <employees>
      <employee name="John Crichton" gender="male" />
      <employee name="Aeryn Sun" gender=female" />
    </employees>
I believe that was OP's point. Using the annotation you cannot add an attribute with subattributes. What if you wanted to add the employee's last six salaries. Suddenly you need to restructure your xml:

    <employees>
      <employee>
        <name>John Crichton</name>
        <gender>male</gender>
        <salaries>
            <salary>10000</salary>
            <salary>10000</salary>
            <salary>10000</salary>
        <salaries>
      </employee>
      <employee>
        <name>Aeryn Sun</name>
        <gender>female</gender>
      </employee>
    </employees>
Using JSON, you just so: “salaries”: [10000,10000,10000]

JSON is not only less verbose, it is also more flexible and easier to read and understand. You don’t have to worry about should I use tags or attributes for that because I late might have to use sub tags, and that makes it far easier to use (and honestly parse as well because many XML documents I have gotten are very inconsistent).

Why do you need to restructure the xml? You can just keep the attributes. To add the salaries there are quite a few options, as XML is just as flexible as well.

Using a List (XSD List) type:

    <employees>
      <employee name="John Crichton" gender="male" salaries="10000 10000 1000" />
      <employee name="Aeryn Sun" gender="female" />
    </employees>
And using mixed content (complexType):

    <employees>
      <employee name="John Crichton" gender="male">
        <salary>10000</salary>
        <salary>10000</salary>
        <salary>10000</salary>
      </employee>
      <employee name="Aeryn Sun" gender="female" />
    </employees>
Still more typing and less readable than the JSON. I happily used XML for years and have nothing against it. But JSON is almost always more readable. Big deal. Technology evolves. Trends that aren't an evolution usually fade. JSON does not seem to be one of those. It offers enough advantages over XML to have quickly replaced it in many cases.
Wether it is easier to read or not is entirely subjective. There is no quantifiable measure to state either way. Both have their use-case; one now more than the other.