JSON and XML are both formats for receiving data from a web server.
The JSON and XML examples below both define an “employees” object containing an array of three employee records:
{“employees”:[ { “firstName”:“John”, “lastName”:“Doe” }, { “firstName”:“Anna”, “lastName”:“Smith” }, { “firstName”:“Peter”, “lastName”:“Jones” } ]} |
<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> |
Key Difference:
XML requires an XML parser, while JSON can be parsed with a standard JavaScript function.
Parsing XML is more complex compared to JSON. JSON is directly parsed into a ready-to-use JavaScript object. |
For AJAX applications, JSON is faster and simpler than XML:
With XML:
With JSON:
JSON.parse()
.