Both JSON and XML may be used to acquire information from a web server.
The following JSON and XML examples each define an employees object, with an array of three employees:
Example of JSON
{"employees":[ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]}
Example of XML
<employees> <employee> <firstName>John</firstName> <lastName>Doe</lastName> </employee> <employee> <firstName>Anna</firstName> <lastName>Smith</lastName> </employee> <employee> <firstName>Peter</firstName> <lastName>Jones</lastName> </employee> </employees>
The largest distinction is:
XML needs to be parsed with an XML parser. JSON may be parsed via means of a popular JavaScript function.
XML is a good deal and more difficult to parse than JSON.
JSON is parsed right into a ready-to-use JavaScript object.
For AJAX applications, JSON is quicker and easier than XML:
Using XML
Using JSON
JSON and XML are human-readable formats and are language-independent. They each have guides for creation, reading, and decoding in actual global situations. We can compare JSON with XML, primarily based totally on the subsequent factors −
Verbose
XML is greater verbose than JSON, so it's miles quicker to jot down JSON for programmers.
Arrays Usage
XML is used to explain the established data, which would not consist of arrays while JSON consists of arrays.
Parsing
JavaScript's eval approach parses JSON. When carried out to JSON, eval returns the defined object.
Below is the short and clear differentiation of JSON and XML
JSON 1. It is JavaScript Object Notation 2. It is a way of representing objects. 3. Its files are very easy to read as compared to XML 4. It doesn’t support comments. | XML It is an Extensible markup language It is a markup language and uses a tag structure to represent data items. Its documents are comparatively difficult to read and interpret. It supports comments. |