All about JSON – JavaScript Object Notation


JSON is a text format to exchange data

JSON is a text format that has been developped for exchange of data between different programming languages or IT components. A typical example is the data exchange between a webserver and a browser. JSON has been standardized by ECMA International in 2013. The standard can be found at The JSON Data Interchange Format, Standard ECMA-404, 1st Edition / October 2013.

Perhaps the most popular website on JSON is www.json.org.

JSON is short for JavaScript Object Notation. The format was inspired by JavaScript object literals, hence, the name of the format. This does not mean that JSON can only be used in JavaScript. JSON is a text format. As (probably) all programming languages can handle texts, JSON can be used to exchange data between any two programming languages. If a text conforms to the JSON format, it is said to be a JSON text. Here is an example of a JSON text.

{
"Name":{
"Last":"Adem",
"First":"Jan"
},
"DateOfBirth":{
"Year":1976,
"Month":6,
"Day":17
},
"DateOfDeath":null
}

The JSON format is very popular. Even to the point that is has become a serious competitor for XML, perhaps thé most used format to exchange data by means of a text between IT components.

JSON is light

The JSON format is said to be a light data exchange format. This means that, often, the JSON text that is used to exchange the data will be shorter than the text that is required by other formats (like XML). This is considered to be a good thing: the less characters that are required to exchange the data, the better. The reason why the JSON text is usually shorter is that the JSON format uses a minimum number of formatting characters. Here is a small example that compares the same data, formatted using JSON and formatted using XML. The formatting characters are put in yellow. The JSON text is shorter than the XML text.

{
"Mail":{
"To":"You",
"From":"Me",
"Body":"Hello!"
}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<Mail>
<To>You</To>
<From>Me</From>
<Body>Hello!</Body>
</Mail>

 

JSON is (human) language independent

A JSON text contains data. An important type of data is text data, for instance, the first name of a person. A JSON text can contain text data coming from any language for which Unicode characters exist. The JSON text itself is written using ASCI characters only. However, by using ASCI characters one can write hexadecimal Unicode code points, like \u00ED, which is the hexadecimal Unicode code point for the character í. The JSON format accepts hexadecimal Unicode code points in text data. As Unicode covers all characters of all human languages, JSON texts can contain text data coming from any human language. Admitted, it is more work to build JSON texts that contain text data that are not in English, as you need to look up the hexadecimal Unicode code points of the special characters. But, the JSON format offers the possibility and, by doing so, the JSON format can be said to be language independent. Here is a JSON text that contains English, Czech (Vítejte), Russian (добро пожаловать) and Chinese (欢迎) text data.

{
"Translation":{
"English":"Welcome",
"Czech":"V\u00EDtejte",
"Russian":"\u0434\u043E\u0431\u0440\u043E\u043F\u043E\u0436\u0430\u043B\u043E\u0432\u0430\u0442\u044C",
"Chinese":"\u6B22\u8FCE"
}
}

Of course, as all programming languages can handle texts, the JSON format is computer language independent.

A JSON text is not unique

A JSON text is never unique. There always exist other JSON texts that contain (and exchange) exactly the same data. First, you can add whitespace (tabs, spaces, end-of-line characters) to a JSON text (at the appropriate places) without changing the data exchange. The JSON text changes, but the data exchange doesn't. This is an interesting feature of the JSON format. It allows to visualize JSON texts in a nice way, which makes them more readable for humans. There are a number of handy websites that make JSON texts easy to read, like jsonlint.com or jsonprettyprint.com. Second, the order of the JSON values in a JSON object does not matter. If a JSON text contains a JSON object with multiple JSON values, the order can be changed. This, of course, changes the JSON text, but not the data exchange. The following three JSON texts contain (and exchange) exactly the same data.

{"Guacamole":{"Ingredients":["Avocado","Lime","Shalot","Tomato","Salt","Cayenne Pepper"],"Recipe":"Mix everything"}}
{
"Guacamole":{
"Ingredients":[
"Avocado",
"Lime",
"Shalot",
"Tomato",
"Salt",
"Cayenne Pepper"
],
"Recipe":"Mix everything"
}
}
{
"Guacamole":{
"Recipe":"Mix everything",
"Ingredients":[
"Avocado",
"Lime",
"Shalot",
"Tomato",
"Salt",
"Cayenne Pepper"
]
}
}

Third, one can even argue that JSON arrays (in which the order of the JSON values does matter) can also be written as JSON objects that contain JSON values with names that represent the order. Then, also the following two JSON texts contain (and exchange) the same data as the three JSON texts above.

{
"Guacamole":{
"Recipe":"Mix everything",
"Ingredients":{
"1":"Avocado",
"2":"Lime",
"3":"Shalot",
"4":"Tomato",
"5":"Salt",
"6":"Cayenne Pepper"
}
}
}
{
"Guacamole":{
"Recipe":"Mix everything",
"Ingredients":{
"5":"Salt",
"1":"Avocado",
"6":"Cayenne Pepper",
"3":"Shalot",
"4":"Tomato",
"2":"Lime"
}
}
}

JSON format is easy to read (by humans)?

It is often claimed that a JSON text is easy to read by a human. In fact, human readability is not what matters. JSON texts are meant to be handled by machines, not humans. For small JSON texts that contain only text data in English and are visualized nicely, the claim may hold. However, in general, JSON texts are not always easy to read by humans. First, the human readability depends on the language in the text data. For a Chinese speaking person, the hexadecimal Unicode code points are quite impossible to read. Second, also the visualization matters. Even for English speaking persons, the claim only holds true on the condition that whitespace (tabs, spaces, end-of-line characters) is put at the appropriate places into the JSON text. Here is a small example: both JSON texts contain exactly the same data. The only difference consists of whitespace. The first JSON text is certainly harder to read than the second one.

{"Transfer":{"From":"000-0000001-01","To":"000-0000002-02","Amount":9.99,"Currency":"AUD"}}
{
"Transfer":{
"From":"000-0000001-01",
"To":"000-0000002-02",
"Amount":9.99,
"Currency":"AUD"
}
}