Comparing JSON and Hyperlambda

JSON and Hyperlambda has a lot of common traits. They’re both relational file formats, allowing you to declare graph objects. JSON of course is understood by any JavaScript clients, simply by being a JavaScript object – Hyperlambda has the advantage of being highly more readable for humans.

I’ve been working on JSON support for Phosphorus Five lately, and in the process realising just how much more readable Hyperlambda is. This is because Hyperlambda has one serious advantage over JSON, which is that while JSON is a key/value relational format – Hyperlambda is a key/value/children relational format. The advantage of this, is difficult to underestimate. Combine this with that Hyperlambda doesn’t require quoting of every darn property, and you don’t have to add curly braces every single time you create an object – And you’ve got the recipe for an extremely more readable format. Which also for most objects that are not too deep, also becomes significantly smaller in size.

Below I have taken one of the JSON examples from Json.Org, and transformed it into Hyperlambda, to allow you to compare the formats for yourself. First the JSON version.

{
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized Markup Language",
          "Acronym": "SGML",
          "Abbrev": "ISO 8879:1986",
          "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": ["GML", "XML"]
          },
          "GlossSee": "markup"
        }
      }
    }
  }
}

Then the Hyperlambda version of the same code.

glossary
  title:example glossary
  GlossDiv
    title:S
    GlossList
      GlossEntry
        ID:SGML
        SortAs:SGML1
        GlossTerm:Standard Generalized Markup Language
        Acronym:SGML
        Abbrev:"ISO 8879:1986"
        GlossDef
          para:A meta-markup language, used to create markup languages such as DocBook.
          GlossSeeAlso
            :GML
            :XML
        GlossSee:markup

Notice how we got rid of every single double quote, except a single pair, which had to be added since the value contained a colon “:”. Notice also how since spacing counts in Hyperlambda, like it does in Python, we’ve entirely gotten rid of every single curly brace “{}”.

The above code has been directly translated form JSON to Hyperlambda. If we wanted to, we could further significantly “pack” it, since Hyperlambda has an additional dimension compared to JSON, which of course is the value being there in addition to its children collection. Let’s do that in fact, and check out the results.

glossary:example glossary
  GlossDiv:S
    GlossList
      GlossEntry:SGML
        SortAs:SGML1
        GlossTerm:Standard Generalized Markup Language
        Acronym:SGML
        Abbrev:"ISO 8879:1986"
        GlossDef:A meta-markup language, used to create markup languages such as DocBook.
          GlossSeeAlso
            :GML
            :XML
        GlossSee:markup

Purely from an informational point of view, the last Hyperlambda above, has not lost a single piece of semantics or data point from the JSON we started out with. Still we were able to reduce 22 lines of JSON to 13 lines of Hyperlambda. We almost got rid of 50% of our overhead! In addition, Hyperlambda is simply superior for being read by humans, obviously!

Below is another one of the examples from Json.org.

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

If we use the newly created (yet unreleased at the time of this writing) Active Event [json2lambda] to convert the above JSON to Hyperlambda, it will yield the following result.

menu
  id:file
  value:File
  popup
    menuitem
      ""
        value:New
        onclick:CreateNewDoc()
      ""
        value:Open
        onclick:OpenDoc()
      ""
        value:Close
        onclick:CloseDoc()

Now in order to preserve the fact that we’re converting from a JSON array, we end up with these funny “” nodes. This is to allow creating JSON back from our Hyperlambda again. However, if we took advantage of the full capability of Hyperlambda, to reduce the above code, we could have illustrated the same structure with the following code, without losing any data at all.

menu:file
  value:File
  popup
    item:New
      onclick:CreateNewDoc()
    item:Open
      onclick:OpenDoc()
    item:Close
      onclick:CloseDoc()

Douglas Crockford, the inventor of JSON once said; “It doesn’t take a genius to create a better file format than XML”. Sorry Douglas, but I feel tempted to return that quote back to you, with the following of my own.

It doesn’t take a genius to create a better file format than JSON.

Don’t get me wrong, JSON is extremely cool, and I am using it internally tons of places in Phosphorus Five – But, unless the recipient of your object for sure is a JavaScript client, it is an extremely inferior format for describing relational structures. At least inferior compared to Hyperlambda.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s