What is JSON?
Let us start with understanding what is JSON.
JSON is a text-based data - interchange format. This means that we can export JSON data to spreadsheets / XML as well as import data from spreadsheets / XML to JSON.
Here are some of examples storing data in JSON format.
1. Simple Values
Let us say we are having a car named 'Santro' which is 'white' in color. The car number is 'XYZ123' and it is a year '2020' model. The information about this car can be stored in the JSON format as given below.
3. Arrays Arrays can also be scored in the JSON format. Given below is an example of a JSON data array of cars.
We have read about 'What is JSON' and its 'JSON Syntax'. Now, we will see why we use JSON. Given below are some of the reasons why we should use a JSON data format.
- It is a lightweight data-interchange format. This means that we can export JSON data to spreadsheets / XML as well as import data from spreadsheets / XML to JSON.
- JSON is faster and its syntax is very easy to understand.
- It is easier to read JSON data as compared to other data formats like XML.
- JSON can also store arrays, objects, and strings in it.
In the section 'What is JSON', we have read about the definition of JSON. Now we will try to understand this with the help of some examples. Given below is a JSON example which consists of the information of an employee working at a company.
Data Types in JSON
In JSON data format, only the following data types can be stored.
- A String: Any string consisting of lowercase, uppercase, numbers as well as special characters can be stored in JSON.
- A JSON object: A JSON object having key-value pairs can be stored in the JSON.
- A Number: Any number can be stored in JSON.
- Boolean values: JSON values can also take boolean i.e. true or false values
- An Array: An array i.e. a list can be also stored in the JSON.
- Null: null represents an empty value that can be stored in a JSON object.
JSON objects allow us to perform nesting of JSON objects. This means that we can also store a JSON object inside another JSON object.
Below is an example to show how the nesting of JSON objects is done.
Here, a JSON object named 'companyDetails' is stored inside another JSON object named 'jsonObject'.
Online Tools for Working with JSON
There are a lot of tools present online for working with JSON. Here are some of the best tools on which you can work with JSON.
1. JSON Viewer Pro JSON viewer pro is a chrome extension that can be used to analyze JSON data. Since it is a chrome extension, it is very easy to understand how to use it. It has a feature to represent the JSON data to a tree or chart based structure.
2. JSON Formatter JSON formatter is a free tool on which users can practice and work with JSON data. It also has a feature to convert JSON data to other formats such as .csv, XML, etc.
3. JSON Editor Online JSON editor online is another free tool to work on JSON data. It has a feature to compare two JSON files. It also has a feature to represent the JSON data into a tree-based structure.
JSON schema is a formal representation of the keys and the datatype of values in the key-value pairs which are stored in a JSON object. It describes how a particular JSON object is structured, and what attributes and data types it contains.
Why is the JSON Schema used?
JSON schema is used to validate a JSON object. JSON data is said to be valid with respect to a JSON schema if all the attributes in the JSON data along with their data types match exactly with the JSON schema.
For example, valid JSON data with respect to the schema defined above is given below.
An invalid JSON data with respect to the schema defined above is given below.
Obtaining the JSON
Populating the Header
For example we have the JSON data of cars which are given below.
The car's 'name' and the car's 'number' can be populated to the header of an HTML page by using the code given below.
- We use JSON because it is a lightweight data-interchange format, it has easy-to-understand syntax and it can be also used in other programming languages.
- JSON can store a JSON object, a string, a number, a boolean value, an array, and a null value.
- JSON Viewer Pro, JSON Validator and JSON Editor Online are some of the free tools that can be used to work on JSON data.
- A JSON schema is a formal representation that describes how a particular JSON object is structured, and what attributes and data types it contains.