JavaScript Object Notation (JSON)
- JSON is a lightweight text-based open standard designed for human-readable data interchange.
- It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available like most programming languages.
- JSON is syntax for storing and exchanging data. JSON is text, written with JavaScript object notation.
- When exchanging data between a browser and a server, the data can only be text.
- JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.
- We can also convert any JSON received from the server into JavaScript objects.
- This way we can work with the data as JavaScript objects, with no complicated parsing and translations.
Why Use JSON over XML?
Earlier days XML used as only option available for data exchange. Various developments in the field of data processing and interchanging have made JSON as an emergent alternative to XML due to number of reasons, such as
- JSON is lightweight in comparison with XML.
- JSON parses data faster than XML by using standard JavaScript function. JSON is parsed into a ready-to-use JavaScript object.
- XML is much more difficult to parse than JSON by using XML parser.
- JSON has a better ratio of data to markup.
- Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.
- JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:
JSON.parse()
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.
- For AJAX applications, JSON is faster and easier than XML which include only fetching a JSON string and parsing that string.
- JSON require less tags than XML – XML items must be wrapped in open and close tags whereas in JSON just name the tag once.
- Because JSON is transportation-independent, you can just bypass the XMLHttpRequest object for getting your data.
- Data in XML is arranged in a tree format, which is not always the same structure as the original data. JSON scores over XML with its map data structure that matches with the actual data which make the interpretation easy and data arrangement more predictable.
- JSON data structure helps in building RESTful APIs providing simpler data exchange methods.
- JSON applies the same data representation method as done in the code. Hence, many languages can just slurp in JSON to find their own domain object with ease. JSON allows easy transition of its objects from JSON object to the objects in code. In XML, this transition is difficult because there is no alignment between the objects in XML and those in the code.
- JSON has a limited set of objects that can be modeled. So, in JSON developers can create simple, more predictive and easy-to-read code. Whereas in XML, we have more objects that can be modeled and developers are unrestricted in using any of the user-defined objects. This makes XML more complex and difficult to read.