Van Morrison - October 9, 2018
What is JSON?
JSON is the abbreviation of JavaScript Object Notation, which represents a standardized syntax for storing and sharing data among entities, derived from the JavaScript programming language (not to be confused with the Java programming language).
JSON gained popularity because it is easy to read and edit by humans and also easy to use by machines. Another advantage is that JSON it is not bound to the JavaScript language, it can be used by a wide range of programming language, most notable Java, PHP, Python and Ruby.
The JSON entity can exist either as a standalone file, usually having the .json extension, or included in another file, inside of quotes, as a JSON string. It can also be assigned to variables.
JSON, compared to XML, offers a better readability, less headache for formatting and has a smaller footprint, causing less bytes to be transferred.
Structure
The structure is extremely simple:
“name” : value
The name must always be a string. Example: “age”, “Birth_date”, “Birth Date”, “2nd address”.
The value can be of different types, Boolean, number, string, array, object or null. Example:
{
“user” : “Emma”,
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”],
“vpn” : false,
“userID” : 215
}
Explanation:
{
“user” : “Emma”, <- this is a string
“connection” : “secured”, <- this is also a string
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”], <- this is a list of strings
“vpn” : false, <- this is a Boolean
“userID” : 215 <- this is a number
}
There are only a few basic rules you must understand in order to use JSON, everything else will come natural.
– the JSON object must be enclosed between curly brackets, even if it is nested (I’ll explain later what nested means) { }
– the name must be between quotations “ “
– the name and the value must be separated by a colon :
– values:
– strings must be between quotations “ ”
– Booleans and numbers are written with no punctuation marks
– lists must be between square brackets [ ]
– between pairs of ‘name : value’ there must be a comma ,
JSON does not care for whitespace between its elements, so you can format your code in multiple ways:
{
“user” : “Emma”,
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”],
“vpn” : false,
“userID” : 215
}
is the same as
{ “user” : “Emma”, “connection” : “secured”, “proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, FTP”], “vpn” : false, “userID” : 215 }
or as
{ “user” : “Emma”, “connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, FTP”], “vpn” : false,
“userID” : 215 }
In order to make the human component more comfortable, you will see most of the JSON entities written as in the first example, each element on one line.
Nested Elements
So far, so good, nothing very complicated, but in order to achieve complex utilities, we can combine the elements even more.
Nested Objects
It makes no sense to create a JSON for each user when we can store multiple users in the same JSON. In the below example, for each user, a nested object exists, highlighted in different colors:
{
“Emma” : {
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”],
“vpn” : false,
“userID” : 215
},
“John” : {
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “FTP”],
“vpn” : true,
“userID” : 216
},
“Sarah” : {
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”],
“vpn” : true,
“userID” : 217
},
“Michael” : {
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”],
“vpn” : false,
“userID” : 218
}
}
Notice the commas between the elements, after each curly braces. It is a syntax rule.
For more complex usage, JSON supports nested arrays. The arrays can contain multiple data types. Two nested arrays are used and highlighted here:
{
“user” : “Emma”,
“connection” : “secured”,
“proxy_options” : [“SOCKS5”, “SSL”, “HTTP”, “FTP”],
“vpn” : false,
“userID” : 215,
“services” : [
{
“serviceID” : “P1388”,
“purchased” : “03-08-2018”,
“active” : true
},
{
“serviceID” : “P1903”,
“purchased” : “05-09-2018”,
“active” : true
}
]
}
JSON vs XML
While the XML format is still being used, JSON is now preferred for data marshalling. In a more and more online based human operations and interactions, it is normal for JSON to gain ground. First off all, JSON is more efficient than XML, I’ve went trough some reasons in this article. Secondly, all modern browsers offer builtin JSON support.
Short example:
JSON
{“clients”:
[
{“name”:“Tim”, “email”:“tim@email.org”},
{“name”:“Drew”, “email”:“drew@email.org”},
{“name”:“Carla”, “email”:“carla@email.org”}
]
}
XML
<clients>
<client>
<name>Tim</name>
<email>tim@email.org</email>
</client>
<client>
<name>Drew</name>
<email>drew@email.org</email>
</client>
<client>
<name>Carla</name>
<email>carla@email.org</email>
</client>
</clients>
Let’s sum up the other reasons why JSON is widely preferred to XML:
– it is easier to read and write
– it is easier to learn
– it supports arrays
– it stores the same data in less disk space, also using less network bandwidth
XML also has some advantages over JSON:
– it allows the user to display data because it’s a markup language
– it supports more data types. Along text, it supports graphs, charts, images etc.
– it supports comments
After reading this article and depending on your purpose, I am convinced you know which format suits your project.