If you work with APIs, configuration files or data exports, you will meet two formats again and again: JSON and XML. Both describe structured data as plain text, both are readable by humans and machines, and both are supported by every major programming language. Yet they were designed with different goals, and choosing the wrong one can make a project harder than it needs to be.
This guide shows the same data in both formats, explains how each one works, compares them point by point and ends with practical advice on when to use which.
The same data in JSON and XML
Here is a small record describing a book, first in JSON:
{
"id": 42,
"title": "Learning to Code",
"available": true,
"price": 19.99,
"authors": ["Priya Nair", "Tom Hale"],
"publisher": {
"name": "Northwind Press",
"country": "India"
}
}
And here is the same record in XML:
<?xml version="1.0" encoding="UTF-8"?>
<book id="42">
<title>Learning to Code</title>
<available>true</available>
<price currency="USD">19.99</price>
<authors>
<author>Priya Nair</author>
<author>Tom Hale</author>
</authors>
<publisher>
<name>Northwind Press</name>
<country>India</country>
</publisher>
</book>
Even in this short example, a few differences stand out. JSON is shorter because it has no closing tags. JSON knows that 42 is a number and true is a boolean, while in XML everything inside a tag is text until a schema or your code says otherwise. XML, on the other hand, can attach attributes such as currency="USD" to an element, which JSON has no direct equivalent for.
How JSON works
JSON (JavaScript Object Notation) is built from just two structures: objects, which are collections of key and value pairs inside curly braces, and arrays, which are ordered lists inside square brackets. Values can be strings, numbers, booleans (true or false), null, objects or arrays. That is the whole format.
The rules are strict but few:
- Keys must be strings in double quotes.
- Strings use double quotes, never single quotes.
- No trailing comma after the last item in an object or array.
- No comments are allowed in standard JSON.
Because the format is so small, parsers are fast and every language has one built in or available as a standard library. In JavaScript, JSON.parse() turns text into an object and JSON.stringify() does the reverse. If you get an error, paste your data into the JSON Formatter & Validator; it points to the line and character where parsing failed.
How XML works
XML (Extensible Markup Language) is a markup language, a cousin of HTML. Data lives in elements marked by opening and closing tags, and elements can carry attributes. A document has exactly one root element, and every tag must be properly closed and nested.
XML comes with a large family of related standards:
- XSD (XML Schema) describes exactly which elements, attributes and data types a document may contain, so documents can be validated automatically.
- Namespaces let documents mix vocabularies from different sources without name clashes.
- XPath and XQuery select and query parts of a document.
- XSLT transforms one XML document into another, or into HTML.
This ecosystem is why XML remains common in publishing, finance, government data exchange and enterprise systems, where documents must follow precise, long-lived contracts.
Side-by-side comparison
| Feature | JSON | XML |
|---|---|---|
| Size | Compact, no closing tags | Larger because of opening and closing tags |
| Data types | String, number, boolean, null, object, array | Text by default; types come from a schema |
| Arrays | Built in with [ ] |
Represented by repeating elements |
| Attributes | Not supported | Supported |
| Comments | Not allowed | Allowed with <!-- --> |
| Validation | JSON Schema (optional, separate standard) | DTD or XSD, deeply integrated |
| Querying | JSONPath, jq, or your language’s code | XPath, XQuery |
| Mixed content (text with markup inside) | Awkward | Natural, as in HTML-like documents |
| Typical use today | Web and mobile APIs, config files, NoSQL databases | Documents, SOAP services, office file formats, feeds, enterprise integration |
Why most new web APIs use JSON
When browsers and mobile apps became the main consumers of APIs, JSON had clear advantages. It maps directly to the objects and arrays programmers already use, so there is almost no conversion code. Payloads are smaller, which matters on slow mobile networks. And JavaScript, the language of the web, can read it natively.
That is why REST APIs from payment providers, social networks, cloud platforms and most SaaS products return JSON by default. If you are building a new web or mobile API with no special requirements, JSON is almost always the right default.
Where XML is still the better choice
XML is not outdated; it is specialised. It remains the stronger option when:
- You are describing documents rather than data. Formats such as DOCX, XLSX and SVG are XML underneath because text, formatting and structure are mixed together.
- A strict contract matters. Banks, insurers and governments often require XSD-validated messages so every partner sends exactly the same structure.
- You need metadata on values. Attributes like units, currency or language sit naturally on an element.
- An existing system expects it. SOAP web services, RSS and Atom feeds, Android layout files and many sitemaps are XML.
Converting between JSON and XML
Conversion is possible but rarely perfect, because the two models do not match exactly. Watch out for these traps:
- Attributes: converters usually turn them into keys with a prefix such as
@idor_id. Agree on one convention. - Single-item arrays: an XML element that appears once looks the same as a single value, so a converter may produce a string where you expected an array. Your code should handle both, or use a schema-aware converter.
- Types: XML text such as
"19.99"becomes a string in JSON unless you convert it. - Order: XML preserves element order; JSON objects are formally unordered.
After converting, run the result through the JSON Formatter to check it is valid and readable.
Quick decision guide
- Building a new REST API for a website or app? JSON.
- Writing a configuration file people will edit by hand? JSON (or YAML/TOML if comments matter).
- Exchanging regulated messages with banks or government systems? XML, usually with a required schema.
- Storing rich documents with formatting? XML-based formats.
- Integrating with an older SOAP service? XML, because that is what it speaks.
Frequently asked questions
Is JSON faster than XML?
Usually yes for typical API payloads, because it is smaller and simpler to parse. The difference only matters at large volumes; choose based on requirements first.
Can JSON have comments?
Standard JSON cannot. Some tools accept “JSON with comments”, but a strict parser will reject them, so remove comments before sending data to an API.
Is XML more secure?
Neither format is secure by itself. XML parsers need care to avoid issues such as external entity (XXE) attacks, so disable external entities unless you need them. With JSON, never evaluate data as code; always use a proper parser.
Need sample data to practise with?
The Fake API & Mock Data Generator returns realistic JSON in simple, medium and complex structures, and the JSON Template Creator gives you ready-made starting structures.


