JSON stands for Javascript object notation. It is a lightweight text format for storing and transporting data.
It is easy for humans to read and write. It is easy for machines to parse and generate.
{
"squadName": "Super hero squad",
"formed": 2016,
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
},
]
}
History of JSON
Douglas Crockford originally specified the JSON format in the early 2000s. He and Chip Morningstar sent the first JSON message in April 2001.
Before JSON
Before JSON was introduced, data is transported in XML format. Above JSON can be written in XML as
<superHeroSquad>
<squadName>Super hero squad</squadName>
<formed>2016</formed>
<active>true</active>
<members>
<member>
<name>Molecule Man</name>
<age>29</age>
<powers>
<power>Radiation resistance</power>
<power>Turning tiny</power>
<power>Radiation blast</power>
</powers>
</member>
</members>
</superHeroSquad>
JSON
JSON is a data representation format based on the subset of the Javascript Programming Language Standard ECMA-262 3rd Edition.
When sending JSON data over a network, it is serialized into a string format, which can then be transmitted using various protocols such as HTTP, WebSocket, or others. Upon receiving the JSON string, the recipient can deserialize it back into a data object in their programming language of choice for processing. JSON filenames use the extension .json
.
JSON structure
- A collection of name/value pairs: In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values: In most languages, this is realized as an array, vector, list, or sequence
JSON Syntax Rules
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
Arrays as JSON
We can also convert arrays to/from JSON. Below is also valid JSON, for example:
[
{
"name": "Molecule Man",
"age": 29,
"powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
},
]
Advantage of JSON
- self describing and easy to understand
- programming language independent
- Its syntax is very small, easy, and light-weighted that’s the reason it executes and responds in a faster way.
- It has a wide range for browser support compatibility with the operating systems. It doesn’t require much effort to make it all browser compatible.
Disadvantages of JSON
- There is no error handling in JSON. If there was a slight mistake in the script then you will not get the structured data.
- It has limited supported tools that we can use during the development.
Top comments (3)
Hi Sudip,
One big difference between XML and JSON is the lack of a standardised schema language. For XML there was XSD but, to my knowledge, the best we have is json-schema.org/, which is not standardised.
To add - one major disadvantage of JSON is formatting. It doesn't support comments and quote symbols have to be escaped explicitly and many other formatting quirks that made JSON a bit dated. It's also a very heavy in terms of file size though compresses well.
@wraptile you are right. Thanks for pointing it out