SKU // 123

JSON vs XML

Before JSON became the favorite, XML was the standard way to send structured data, and you still meet it in feeds, vector graphics, and office documents. Seeing the same data in both formats shows why JSON took over the web, and where XML still earns its place.

// The Same Data, Two Ways

{
  "student": {
    "name": "Ava",
    "grade": 10,
    "clubs": ["robotics", "chess"]
  }
}
<student>
  <name>Ava</name>
  <grade>10</grade>
  <clubs>
    <club>robotics</club>
    <club>chess</club>
  </clubs>
</student>

Both describe the same student. XML marks every piece with an opening and a closing tag, and it has no built-in idea of an array, so the list of clubs becomes a <clubs> element containing several <club> elements.

// XML in Thirty Seconds

// Side by Side

JSON XML
SizeShorterLonger (every value needs two tags)
ArraysBuilt in: [ ]Repeat an element
Typesstring, number, boolean, nullEverything is text
CommentsNoneYes: <!-- note -->
AttributesNo such thingYes, plus namespaces
Reading it in JSJSON.parse, then plain objectsA parser that builds a document tree
Typical homeWeb APIs, settings, saved dataFeeds, SVG, office files, older services

// JSON Is Smaller

const json = '{"name":"Ava","grade":10}';
const xml = "<student><name>Ava</name><grade>10</grade></student>";
console.log(json.length);
console.log(xml.length);
25 52

On a big list of records the difference adds up, which matters for loading time on a phone connection.

// Reading JSON in JavaScript

JSON becomes ordinary objects and arrays with one call, because JSON was modeled on JavaScript values:

const data = JSON.parse('{"name":"Ava","clubs":["robotics","chess"]}');
console.log(data.name);
console.log(data.clubs[1]);
Ava chess

// Reading XML in JavaScript

XML needs a parser that builds a document. The browser has one called DOMParser. You then search the document with querySelector and read text with textContent, or an attribute with getAttribute:

const text = `<student id="7"><name>Ava</name><clubs><club>robotics</club><club>chess</club></clubs></student>`;
const doc = new DOMParser().parseFromString(text, "application/xml");

console.log(doc.querySelector("name").textContent);
console.log(doc.querySelector("student").getAttribute("id"));
console.log(doc.querySelectorAll("club").length);
console.log(doc.querySelectorAll("club")[1].textContent);
Ava 7 2 chess

Notice that the id comes back as the text "7", not the number 7. XML values are always text until you convert them.

// Bad XML Does Not Throw

Unlike JSON.parse, DOMParser does not throw an error for broken XML. It quietly returns a document that contains a parsererror element, so you have to look for it:

const bad = "<student><name>Ava</student>";
const doc = new DOMParser().parseFromString(bad, "application/xml");
console.log(doc.querySelector("parsererror") !== null);

const good = "<student><name>Ava</name></student>";
const ok = new DOMParser().parseFromString(good, "application/xml");
console.log(ok.querySelector("parsererror") !== null);
true false

// Why JSON Won the Web

// Where XML Still Wins

XML is not gone. It suits documents (text mixed with markup), data that needs comments or attributes, and tools built on its strict rules:

Choosing
For data moving between your page and a server today, use JSON. Reach for XML when a tool or service you must work with already speaks XML.

// Quick Check

Which statement about XML is true?

  1. It has built-in numbers and booleans
  2. Every value is written inside an opening and a closing tag
  3. It cannot have attributes
  4. It is read with JSON.parse
Show answer
B. XML marks each piece with a pair of tags. Everything in it is text, it supports attributes, and it needs its own parser.

Name two reasons JSON replaced XML for most web APIs.

Show answer
Any two of: JSON is shorter, it maps directly onto JavaScript objects and arrays, it needs only JSON.parse, and it is easy for people to read.

What does this log?

const doc = new DOMParser().parseFromString('<a id="5"></a>', "application/xml");
console.log(typeof doc.querySelector("a").getAttribute("id"));
Show answer
string. XML attribute values are always text, so "5" is a string. Convert it with Number(...) if you need a number.

// Try It

  1. Click Open Playground and run it. Which format gives the shorter text?
  2. Write the XML for a pet with a name and two toys, then read the second toy with querySelectorAll
  3. Break the XML by removing a closing tag and detect the problem with parsererror
  4. Find one place you have seen XML in real life (an RSS feed, an SVG image, a sitemap) and look at its source
// Video Walkthroughs
Last updated: Oct 11, 2026, 6:18 PM ET