3.1. JSON Introduction
JavaScript Object Notion which is commonly known as JSON is one of the most popular data transition formats. It is a text-based and lightweight format for data transactions. JSON format was first computed by Douglas Crockford. This being a text-based format is easier to read or write by the user and at the same time, its lightweight property makes it a stress-free alternative for machines to deconstruct or generate. It is basically a subset of the JavaScript but JSON, as a text format is totally independent of any of the programming languages used as almost all the languages, can easily analyze the text. Its unique properties like text-based, lightweight, language independence etc. make it an ideal candidate for the data-interchange operations.
3.1.1 Syntax and Structure
A JSON object is a key-value data format that is typically rendered in curly braces. When you’re working with JSON, you’ll likely see JSON objects in a .json file, but they can also exist as a JSON object or string within the context of a program.
A JSON object looks something like this:
{
"first_name" : "John",
"last_name" : "Doe",
"location" : "Earth",
"online" : true,
"followers" : 256
}
Although this is a very short example, and JSON could be many lines long, this shows that the format is generally set up with two curly braces (or curly brackets) that look like this { }
on either end of it, and with key-value pairs populating the space between. Most data used in JSON ends up being encapsulated in a JSON object.
Key-value pairs have a colon between them as in "key" : "value"
. Each key-value pair is separated by a comma, so the middle of a JSON looks like this: "key" : "value", "key" : "value", "key": "value"
. In our example above, the first key-value pair is "first_name" : "John"
.
JSON keys are on the left side of the colon. They need to be wrapped in double quotation marks, as in "key"
, and can be any valid string. Within each object, keys need to be unique. These key strings can include whitespaces, as in "first name"
, but that can make it harder to access when you’re programming, so it’s best to use underscores, as in "first_name"
.
JSON values are found to the right of the colon. At the granular level, these need to be one of 6 simple data types:
- strings
- numbers
- objects
- arrays
- Booleans (true or false)
- null
At the broader level, values can also be made up of the complex data types of JSON object or array. Each of the data types that are passed in as values into JSON will maintain their own syntax, so strings will be in quotes, but numbers will not be.
Though in .json
files, we’ll typically see the format expanded over multiple lines, JSON can also be written all in one line.
{ "first_name" : "John", "last_name": "Doe", "online" : true, }
This would be more common within another file type or when you encounter a JSON string. Writing the JSON format on multiple lines often makes it much more readable, especially when dealing with a large data set. Because JSON ignores whitespace between its elements, you can space out your colons and key-value pairs in order to make the data even more human readable:
{
"first_name" : "John",
"last_name" : "Doe",
"online" : true
}
It is important to keep in mind that though they look similar, a JSON object is not the same format as a JavaScript object, so, though you can use functions within JavaScript objects, you cannot use them as values in JSON. The most important attribute of JSON is that it can be readily transferred between programming languages in a format that all of the participating languages can work with. JavaScript objects can only be worked with directly through the JavaScript programming language.