A bit about JSON

JSON (Java Script Object Notation) is a really popular, lightweight data-interchange format. I don’t think I would be wrong by saying that it’s fast becoming the data-interchange format.

Photographer Robert Knapp www.modernartphotograph.com
Photographer Robert Knapp www.modernartphotograph.com

What are data-interchange formats?

Data-interchange formats are agreed upon specifications that allow data to be sent between systems (or even between components of the same system).

If System A expects to get apples and we send coffee beans (which isn’t even a fruit) System A will have no idea how to handle our coffee beans. I think we should still all agree that coffee beans are superior to apples.

Sometimes our systems are made up of different components and those components also need to communicate with each other, by agreeing that we are going to send a certain type of message (not the message content, but the format) between components within our systems developers can start writing code, teams can make progress independent of each other, the only thing individual developers will need to do is generate a few mock objects in the agreed format and stub the inputs into their components. Also they only need to ensure that their components output the correct format, with the correct data in their tests (integration work between components and teams still needs to happen, but it becomes easier with a predefined format).

XML, JSON and YAML are examples of data-interchange formats.

How does JSON look?

{
"music-man" : { 
 "first-name" : "Bob",
 "last-name" : "Dylan",
 "age" : 74,
 "instruments" : ["Vocals", "Guitar", "Keyboards", "Harmonica"],
 "favouriteBeverage" : "probably Coffee"
 }
}

Above is an example of how a JSON object would look, here we are defining a “music-man” object. The properties of that object are first-name, last-name, age, instruments (which is an array) and a favouriteBeverage (which is probably coffee, why wouldn’t it be?).”

Why is JSON so cool?

1. It’s succinct

Unlike XML which contains a lot of meta data, JSON is very succinct. It says what needs to be said and no more. This makes JSON very lightweight, it doesn’t like to waste space with unnecessary opening and closing tags.

2. Human readable

JSON is a lot more human readable, XML with all it’s extra data is very difficult to read. JSON objects describe themselves well in a small, easy to understand format.

3. Most programming languages support it

I believe that all of the major languages have some form of JSON parser. Feel free to leave a comment if there is a programming language that doesn’t support JSON.

What should I look out for?

1. There is no formal schema

While technically this isn’t true (JSON Schema), very few people use JSON schemas. So when integrating with services you have to ensure that your JSON is generated correctly.

Not having an official schema can be good and bad, it’s flexible which can be a benefit or a hinderance depending on your specific situation.

2. Difficult to compose by hand

With all it’s commas and inverted commas, JSON can sometimes be a pain to compose by hand. There are however some really good tools to help you avoid having to do this manually (Json Parser Online).

Before I go

This is slightly off topic (but not really).

I was lucky to have found a really cool blog called simpleprogrammer.com, the author John Sonmez really does a good job talking about things that programmers often forget or neglect. I really enjoy the material he is creating and I noticed that he was giving away a free blogging course so I signed up (click here if you are interested).

He shares a lot of really valuable advice, stuff that took him years to figure out, on how to get the most out of your blog (he also takes the time to reply to your “homework” emails), I have a blog coming up about something I really started thinking about after one of his emails.

Have a look if you are thinking of writing your own blog (which you should be doing, it’s a lot of fun).

 

Until next time may your human data-exchange be fun and full of happiness.

-R

6 Comments

  1. Reenen said:

    Yeah I don’t generally venture very far away from vba, I recently dipped into League of Legends API where all the data is in JSON.

    Initially I didn’t realise it, so I was parsing it manually (a pain). But soon after I learnt it I was even debugging my code using JSON.stringify.

    The previous new thing I learnt that excited me as much was Dictionaries.

    2015/06/06
    Reply
    • rafal said:

      That’s also a point I should have made.

      Most public APIs send and expect JSON.

      2015/06/08
      Reply
    • rafal said:

      That’s a very useful resource. Thanks for sharing.
      -R

      2015/06/08
      Reply
  2. […] Using some pretty cool magic Angular automatically synchronises data between model and views. So if for example we use the JSON object I wrote about last week: […]

    2015/06/13
    Reply
  3. […] scenario. We have a service which provides us with a list of best-selling albums in JSON (I wrote a bit about JSON here). The data we get looks like […]

    2015/06/20
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *