All you need to know about JSON
The most common format to transfer data over the web
By Martin Helm in Data Science ML Tools
September 10, 2021
Welcome back to my series on markup languages. This time I want to dive into JSON, one of the most common formats to share or serialize data.
JSON stands for JavaScript Object Notation. As the name already tells, it is derived from JavaScript but is available in many languages. It is designed to be human-readable and light-weight at the same time. Lets start with an example:
{
"name": "Ash",
"profession": "Pokemon Trainer",
"money": 1358,
"pokemon": [
{
"name": "Bulbasaur",
"level": 14,
"attacks": [
"Ram",
"Razor Leaf"
],
"active": true
},
{
"name": "Charmander",
"level": 6,
"attacks": [
"Slash",
"Ember"
],
"active": false
}
],
"Cheats": null
}
The ubiquity of JSON definitely stems from its simplicity, therefore we only need to look at three things:
- Syntax
- Data Types
- Differences to XML
Syntax
A JSON file is saved with the extension .json
and typically starts with an outermost object that encapsulates everything. In theory one can also have an outermost array, which is also valid JSON, but this makes indexing much trickier. Indentation is typically used to make it more human-readable, but there are no strict conventions how many white spaces are used, often it is 2 or 4.
Inside an object, everything is organized as a key-value pair. Keys are always string, so they are within double quotes. One can use white spaces within the key, though it is discouraged as it can become difficult during programming when one wants to access a key. Within an object, every key needs to be unique.
Keys are separated from their values by a colon. This colon can be surrounded by as many white spaces as one wants, as they are ignored when reading out the data. So if you want to really pretty up your JSON document you could also align colons.
The value itself can be one of 6 data types. These can, of course, be nested, to create complex structures. Finally, key-value pairs are separated by commas from each other.
Since comments are not allowed in JSON, you should again aim for self-describing keys.
Data Types
Type | Notation | Characteristics |
---|---|---|
JSON object | { } | Unordered All elements are key-value pairs Can contain values of different types |
Array | [ ] | Ordered Only contains values Can contain values of different types |
String | " " | Always wrapped in double quotes, no single quotes allowed |
Escape character | \ | Used for control characters, such as \n for linefeed |
Boolean | true, false | |
Null | null |
Differences to XML
JSON has several advantages over XML:
First of, it is typically easier to read, because it does not need end-tags. That is also the reason why JSON requires less text, and therefore less space, than XML. Also, basically all programming languages can read JSON directly, whereas XML typically needs dedicated parsers.
In addition, JSON has arrays, whereas XML doesn’t have them and needs to construct several tags with identical names. Compare the following two formats storing the same information:
{"users": [
{"username": "TonyStark", "email": "Tony@avengers.com"},
{"username": "Captain America", "email": "Cap@avengers.com"},
{"username": "Thor", "email": "Thor@avengers.com"},
{"username": "Loki", "email": "Loki@avengers.com"}
] }
<users>
<user>
<username>TonyStark</username> <email>Tony@avengers.com</email>
</user>
<user>
<username>Captain America</username> <email>Cap@avengers.com</email>
</user>
<user>
<username>Thor</username> <email>Thor@avengers.com</email>
</user>
<user>
<username>Loki</username> <email>Loki@avengers.com</email>
</user>
</users>
Summary
And that is already everything there is to JSON. This great simplicity, while still allowing complex structures through nesting, make it so universally applicable. Usually you will not write pure JSON directly, but rather fetch it from a data base or generate it with a program. Still, it is very worthwhile to understand the underlying structure. Below I, also compiled a list of resources that could be helpful for you.
Resources
-
The official introduction to JSON page, with some nice graphical illustrations of the format.
-
A linter to check whether your file adheres to all rules.
-
An XML to JSON converter here