
JSON is a lightweight data format in which we store information in an organized manner. We can access the data JSON format easily.
The full form of JSON is JavaScript Object Notation. The JSON text format does not depend on any programming language. It is programming language independent. We can use this data format in C, C++, C#, Java, JavaScript, Perl, Python and many other languages.
JSON is a collection of name-value pairs, which means we store information as name-value pairs. Most programming languages today support the JSON data structure. These days we are transferring huge amount of data. And the JSON format is the best data structure for this purpose.
Table of Contents
Why should we use JSON?
- JSON format is easy to read and write.
- The syntax of JSON is very simple and very straightforward.
- All major JavaScript frameworks support JSON, so you don’t need to worry about it if you’re using a framework.
- Most importantly, it is language independent. If you have idea about JSON then you can work with JSON in any programming language.
- JSON supports arrays, objects, strings, numbers, and values.
Where does the JSON file come from?
JSON was derived from the JavaScript programming language. But most of the programming languages nowadays have a generator and parser of JSON format data.
Rules for writing JSON format
- You should write the information in name-value pairs like {“Company”:”Geeks Company”}.
- You should use colon (‘:’) to separate name and value.
- If you have multiple data then you should separate the data using comma (“,”).
- If you are preparing data in JSON format, it means you are creating JSON object. You should use curly braces to enclose objects.
- If you need array of data you should use square brackets.
- The JSON format does not allow commas at the end of the last pair-value pair. If you try to add at the end it will come under syntax error of JSON format.
If you are creating JSON information and want to put it in a file, you should have the extension .json. .json extension is not mandatory you can have .txt also.
You can learn Agile Methodology of Software Development.
Example of JSON format file
We are taking example of JSON file which will contain employee information which includes name and email.
Syntax of JSON format
Employee.json
{"employees":[
{"name":"BLAKE", "email":"blake@gmail.com"},
{"name":"CLARK", "email":"clark@gmail.com"},
{"SMITH":"John", "email":"smith@gmail.com"},
{"TURNER":"John", "email":"turner@gmail.com"}
]}
How to open JSON format file?
You can open the JSON file with any text editor like Notepad, Notepad++ etc. If you want to view formatted data you can open json file in Mozilla Firefox (as it is widely used).

Data Types in JSON
The following are the important data types JSON we use frequently:
1) Number
We can use this data type if we want to use a real number, an integer or a temporary number.
{ "age" : 25 }
2) String
If we want to write 0 or more Unicode characters then we can write inside double quotes. And we can also write the escaping character inside double quotes.
{ "name" : "Mark" }
Special characters that we can use in strings of a JSON document
# | Characters | Description |
1 | “” | double quotation |
2 | \ | back slash |
3 | / | forward slash |
4 | b | backspace |
5 | f | form feed |
6 | n | new line |
7 | r | carriage return |
8 | t | horizontal tab |
9 | u | four hexadecimal digits |
3) Boolean
This data type represents either true or false values. Apart from this data type will not support.
{"active" : "true"}
4) Object (JSON Objects)
It is a collection of name-value pairs separated by commas and placed inside curly brackets.
{
"employee":{ "name":"Clark", "age":31, "city":"New York" }
}
5) Array
We enclose the sequence of values in square brackets. And we also separate each value with a comma.
{
"employees":[ "Mark", "Clark", "Smith" ]
}
6) Null
It represents the empty value. Sometimes we do not have value to hold, in which case we use null .
{ "lastname" : null }
Difference between JSON and XML
# | JSON | XML |
1 | JSON stands for JavaScript Object Notation. | XML stands for eXtensible Markup Language. |
2 | JSON object has data type. | XML data is type less. It does not have data type. |
3 | JSON types may be string, number, array, Boolean etc. | But in XML, data should be in string format. |
4 | JSON is easy to learn and simple to read and write. | XML is less easy than JSON and less simple than JSON. |
5 | JSON is data-oriented. | But XML is document-oriented. |
6 | JSON supports array concept. | But XML does not support array. |
7 | JSON is less secured as compared to XML. | It is more secure than JSON. |
8 | You can parse JSON by a standard JavaScript function. | But you can parse XML with an XML parser. |
9 | JSON files are more human readable than XML. | XML files are less human readable than JSON. |
10 | JSON does not support comments. | But XML support comments. |
JSON format
{"Cricketers":[
{ "firstName":"Virat", "lastName":"Kohli" },
{ "firstName":"Kane", "lastName":"Williamson" },
{ "firstName":"M.S.", "lastName":"Dhoni" }
]}
XML format
<Cricketers>
<Cricketer>
<firstName>Virat</firstName>
<lastName>Kohli</lastName>
</Cricketer>
<Cricketer>
<firstName>Kane</firstName>
<lastName>Williamson</lastName>
</Cricketer>
<Cricketer>
<firstName>M.S.</firstName>
<lastName>Dhoni</lastName>
</Cricketer>
</Cricketers>
JSON was initially developed to meet the need for stateless, server-to-browser communication without using browser plugins.
In conclusion, Hope you guys understood the basics about JSON file format. And in this article we have also covered the syntax of JSON with supported data type, example of JSON. Apart from this, you also got an idea of the difference between JSON and XML which are two different types of file formats.