TExcelTAD
Home › JSON › JSON vs XML

JSON vs XML

Before JSON took over, XML (eXtensible Markup Language) was the standard way to exchange data between systems. Both formats are text-based, human-readable, hierarchical and supported everywhere — but they have very different philosophies.

The same data in both formats

A list of two employees in XML:

xml
<employees>
  <employee id="1">
    <name>Mai</name>
    <role>Designer</role>
    <remote>true</remote>
  </employee>
  <employee id="2">
    <name>Quang</name>
    <role>Developer</role>
    <remote>false</remote>
  </employee>
</employees>

And in JSON:

json · try it yourself
{
  "employees": [
    {"id": 1, "name": "Mai", "role": "Designer", "remote": true},
    {"id": 2, "name": "Quang", "role": "Developer", "remote": false}
  ]
}
Valid JSON: object with 1 key
{
  "employees": [
    {
      "id": 1,
      "name": "Mai",
      "role": "Designer",
      "remote": true
    },
    {
      "id": 2,
      "name": "Quang",
      "role": "Developer",
      "remote": false
    }
  ]
}

The JSON version is shorter because there are no closing tags, and it already knows that 1 is a number and true is a boolean. In XML, everything is text until your program decides how to interpret it.

How they are similar

  • Both are self-describing: field names travel with the data.
  • Both are hierarchical: values can contain other values.
  • Both can be parsed by practically every programming language.
  • Both can be fetched over HTTP.

How they differ

JSON XML
Syntax Braces, brackets, "key": value Opening and closing tags
Data types string, number, boolean, null, object, array Everything is text (types only via a schema)
Arrays Built in: [ ] Repeated elements by convention
Attributes No — just more keys Yes: <employee id="1">
Comments Not allowed <!-- ... -->
Namespaces No Yes
Size Compact Verbose
Parsing in JS JSON.parse() — one call DOM parser, then walk the tree
Schema languages JSON Schema XSD, DTD, RelaxNG
Typical use today Web APIs, configs, NoSQL Documents (SVG, XHTML, Office files), SOAP, RSS, enterprise systems

Why JSON is easier in code

With JSON, one function call gives you ready-to-use objects and arrays:

js · try it yourself
const text = `{
  "employees": [
    {"id": 1, "name": "Mai", "role": "Designer", "remote": true},
    {"id": 2, "name": "Quang", "role": "Developer", "remote": false}
  ]
}`;

const data = JSON.parse(text);
for (const e of data.employees) {
  console.log(`${e.id}: ${e.name} (${e.role})${e.remote ? " - remote" : ""}`);
}
const remoteCount = data.employees.filter((e) => e.remote).length;
console.log("Remote employees:", remoteCount);
1: Mai (Designer) - remote
2: Quang (Developer)
Remote employees: 1

With XML you would parse into a DOM tree, search for employee elements, read the id attribute, read the text content of child elements, and finally convert "true" and "1" to real booleans and numbers yourself.

When XML is still the better choice

JSON is not always the winner. XML shines when:

  • The data is really a document with mixed content, like <p>Hello <b>world</b>!</p>.
  • You need namespaces to mix vocabularies (SVG inside XHTML, for instance).
  • You must integrate with an existing XML ecosystem — SOAP services, RSS/Atom feeds, Office Open XML.
  • You need mature tooling such as XSLT transformations or XPath queries.

Mapping XML features to JSON

There is no single official way to convert XML into JSON, but common conventions look like this:

XML Typical JSON
Element with text <name>Mai</name> "name": "Mai"
Attribute id="1" "id": 1 (or "@id": "1")
Repeated elements An array
Empty element <manager/> "manager": null

Here is the attribute-prefix convention some converters use:

json · try it yourself
{
  "employee": {
    "@id": "1",
    "name": "Mai",
    "#comment": "converted from XML"
  }
}
Valid JSON: object with 1 key
{
  "employee": {
    "@id": "1",
    "name": "Mai",
    "#comment": "converted from XML"
  }
}

Tip: When you design a new web API today, choose JSON unless you have a specific reason to need XML's document features.

Test yourself

Which statement about data types is true?

  1. XML has native numbers and booleans
  2. JSON has native numbers, booleans and null
  3. Neither format has types
  4. Both treat everything as text

Which feature does XML have that JSON does not?

  1. Arrays
  2. Nesting
  3. Comments and attributes
  4. UTF-8 support

For which data is XML often a better fit?

  1. A list of products for a REST API
  2. A document with mixed text and markup
  3. A config file for a JavaScript tool
  4. A log line per event