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:
<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:
{
"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:
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:
{
"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?
- XML has native numbers and booleans
- JSON has native numbers, booleans and null
- Neither format has types
- Both treat everything as text
Which feature does XML have that JSON does not?
- Arrays
- Nesting
- Comments and attributes
- UTF-8 support
For which data is XML often a better fit?
- A list of products for a REST API
- A document with mixed text and markup
- A config file for a JavaScript tool
- A log line per event