Introduction to JSON
JSON can be defined as a Java Script Object Notation file format that is used for sending, receiving, and storing the data from the same or different systems in a network. It is generally used in the REST request and response application program interface (API) services, as JSON is uncomplicated and in a readable format. Unlike the traditionally used Extensible Markup Language (XML), it enables faster accessibility, memory optimization is shorter and simpler in nature, and does not contain complicated syntax & tags.
We might have to follow the following syntax rules:
- The data is always in key/value pairs.
- A comma separates every data object.
- The objects are held within the curly braces (the flower bracket like this ‘{}’).
- The arrays are held in square brackets.
JSON Object
A typical object is as follows:
Code:
{
"Name": "Alex",
"Occupation": "Teacher",
"Places visited": ["San Francisco", "California", "Houston"]
}
In the above examples, ‘Name’, ‘Occupation’, ‘Places visited’ are the keys and ‘’Alex’, ‘Teacher’ and the array [“San Francisco”, “California”, “Houston”] are the values of the respective keys and the whole data enclosed in the flower brackets constitutes a JavaScript Object Notation Object. The values can be a string, another object, a Boolean, null, a number.
The objects can also be nested and also be in the form of arrays.
Examples
Below are the examples mentioned:
1. Nested Objects
Code:
{
"Role": "Student",
"Name": {
"First name": "Alex",
"Last name": "Hill"
}
}
The outer curly braces form the primary JSON object nesting to another object with the key ‘Name’ followed by the data in the inner curly braces. Next, let’s see the array of objects.
2. An Array of Objects
Code:
{
"Grade": 6,
"Student names": [
{
"First name": "Alex",
"Last name": "Hill"
},
{
"First name": "Neal",
"Last name": "Hunter"
},
{
"First name": "Peter",
"Last name": "Jones"
}
]
}
As you can see, the outer curly braces form the primary JSON data object, and there is a square bracket pair encloses a set of objects which is the array of objects.
3. Datatypes
Values must have one of the following data types.
- Strings:
Code:
{"name": "Johnny"}
- Number:
Code:
{"age": 21}
- Object:
Code:
{
"employee”: {"name": "John", "age": 221}
}
- Boolean:
Code:
{"sale": true}
4. JSON Parsing
Let us consider Python as the example programming language and see how to handle the data objects.
1. import json
# json is the module in python to handle its data objects
2. string_a = “{“name”: “Alex”, “age”: “22”, “occupation”: “Teacher”}”
# let me explain the example of string format with the syntax rules
3. json_object = json.loads(string_a)
# now the json_object has the string_a as the JSON object, and the key values can be referred like the following
print(“The name is”, json_object[“name”])
print(json_object[“name”, “’s age is”, json_object[‘age’]])
So, in JSON parsing using python, the JSON objects take the form of a dictionary, and the keys will be referred to in the index as mentioned in the above example.
How we can parse the JSON objects from a file in Python
1. import json
# json is the module in python to handle its objects
2. file_handler = open(‘json_data_file.json’, ‘r’)
# open is the function to open a file in python, and the json files are stored with the extension
# .json, which in this example is opened in reading mode ‘r.’
3. json_object = json.load(file_handler)
# json.load() is the function that accepts the file handler of the json file and reads its data
# objects from the file
print(“The name is”, json_object[“name”])
print(json_object[“name”, “’s age is”, json_object[‘age’]])
Exchange of Data Objects
Almost all the programming languages are powered to generate and handle dictionaries or hashes, or associative arrays. So, the data that needs to be exchanged between the clients and the servers needs to be typecast to one of these forms depending on the programming language used.
JSON vs XML
Traditionally, the protocol used to exchange data between the client and the server was SOAP (Simple Object Access Protocol) that used XML (Extensible Markup Language). However, with the introduction of REST (Representational State Protocol), the use of JavaScript Object Notation became widely famous.
Here are a few similarities between them:
- Both are intuitive and easy to read.
- Both can be nested and hierarchical.
- Both can be parsed using a wide variety of languages.
- Both are exchanged over web requests for data transfer.
Here are a few differences between them:
- It has no tag format.
- It is storage optimal as it is relatively shorter than XML.
- It is faster to read and write.
- It can use arrays while it is not easy in XML.
Disadvantages
While we saw the advantages, it is not devoid of disadvantages too.
- It is not fully secure.
- It is limited in terms of the supported data types.
How Knowledge Regarding JSON will help you in your Career?
It is a data format that is used to exchange information between clients and servers. Imagine, if you could develop an optimized schema for data representation that uses fewer data to exchange large data, then that is a great thing. The whole world is moving towards RESTful API services. This is why having a great deal with this language will help you to come up with cool API services.
Conclusion
It is one of the cool data formats for accessing, storing, and comprehending data. If you are looking for something that is quick, and lightweight for data exchange, then you can always look forward to employing JavaScript Object Notation.