JS JSON vs XML
In this page:
Comparing the Same Data in Both Formats
The same structured data looks noticeably different in each format -- JSON's syntax (curly braces, colons, square brackets) closely mirrors JavaScript object and array literals directly, while XML's tag-based syntax more closely resembles HTML, with opening and closing tags wrapping each piece of data.
उदाहरण: Comparing the Same Data in Both Formats
// Declare the constant `json`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `json`, set to `{ name: "Sam", age: 30 }`
const json = { name: "Sam", age: 30 };
// Declare the constant `xml`, set to "<person><name>Sam</name><age>30</age></person>"
// Declare the constant `xml`, set to "<person><name>Sam</name><age>30</age></person>"
const xml = "<person><name>Sam</name><age>30</age></person>";
// Print `JSON.stringify(json)` to the console
// Print `JSON.stringify(json)` to the console
console.log(JSON.stringify(json));
// Print `xml` to the console
// Print `xml` to the console
console.log(xml);
Parsing Complexity: JSON vs XML
JSON.parse() converts a JSON string directly into a usable JavaScript value in one built-in call, while parsing XML requires DOMParser and then navigating the resulting document with DOM methods like querySelector() -- noticeably more steps for the same basic goal of "get the data into a usable form".
उदाहरण: Parsing Complexity: JSON vs XML
const obj = JSON.parse('{"name": "Sam"}');
console.log(obj.name); // one call
const doc = new DOMParser().parseFromString("<name>Sam</name>", "text/xml");
console.log(doc.querySelector("name").textContent); // several steps
Features JSON Lacks That XML Has
XML supports attributes (extra metadata on a tag, like <book id="42">), namespaces (avoiding naming conflicts when combining vocabularies), comments, and mixed content (text interspersed with child elements) -- none of which JSON has a native, direct equivalent for.
उदाहरण: Features JSON Lacks That XML Has
const doc = new DOMParser().parseFromString('<book id="42">Title</book>', "text/xml");
console.log(doc.querySelector("book").getAttribute("id")); // no JSON equivalent
Why JSON Became the Default for Web APIs
JSON's tight fit with JavaScript (no separate parsing library needed), its more compact size (less repeated markup than XML's opening/closing tags), and its simplicity for the vast majority of typical API data (nested objects and arrays, without needing attributes or namespaces) are the main reasons it overtook XML as the default choice for most new web APIs.
उदाहरण: Why JSON Became the Default for Web APIs
console.log(JSON.stringify({ name: "Sam" }).length);
console.log("<person><name>Sam</name></person>".length);
When XML Is Still the Right Choice
XML remains genuinely appropriate for document-centric formats (like RSS feeds, SVG, or Office document formats), systems requiring formal schema validation (XSD), and integration with existing enterprise or SOAP-based systems that were built around it long before JSON became common.
उदाहरण: When XML Is Still the Right Choice
const doc = new DOMParser().parseFromString("<rss><item>News</item></rss>", "text/xml");
console.log(doc.querySelector("item").textContent);
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML