JSON vs XML: Which Should You Use?
The honest summary is that JSON won the argument for APIs and XML kept the jobs JSON was never designed for. That is less exciting than a fair fight, but it is more useful than pretending the choice is balanced.
What follows is what each format actually does differently, the four capabilities XML has that JSON has no answer for, and where the two-way conversion loses information.
The same data in both
{
"user": {
"id": 42,
"name": "Ada Lovelace",
"active": true,
"languages": ["English", "French"]
}
}
And in XML:
<user id="42">
<name>Ada Lovelace</name>
<active>true</active>
<languages>
<language>English</language>
<language>French</language>
</languages>
</user>
Three differences are visible immediately, and they run deeper than they look.
XML has two places to put things. id is an attribute; name is an element. Nothing tells you which a given piece of data should be — it is a modelling decision, and reasonable people make it differently, which is why the same data appears in two shapes across two APIs. JSON has one place: a member of an object.
Types are JSON's, not XML's. In the JSON, 42 is a number and true is a boolean, and a parser gives you them as such. In the XML both are text, and something further down has to know that active should be interpreted as a boolean. XML gets types from a schema, or not at all.
XML repeats itself. Every element names itself twice. On small payloads this hardly matters; on a large array of records it is roughly double the bytes for the same information, before compression.
At a glance
| JSON | XML | |
|---|---|---|
| Kind of thing | Data format | Markup language |
| Standardised as | RFC 8259, ECMA-404 | W3C XML 1.0 (Fifth Edition) |
| Data types | 6, built in | Text only, unless a schema says otherwise |
| Attributes | No | Yes |
| Namespaces | No | Yes |
| Comments | No | Yes |
| Built-in validation | No | Yes, via DTD or XSD |
| Query language | JSONPath (conventional) | XPath (standard) |
| Transformation | Code | XSLT |
| Mixed content | Awkward | Native |
| Verbosity | Lower | Higher |
| Parse in a browser | One call | DOM traversal |
| Typical size | Smaller | 1.5–2× larger |
Why JSON took the API work
Not, mostly, because it is prettier. Three practical reasons, in order of how much they mattered.
It arrives as objects. A JSON response parses into the native data structures of whatever language received it, in one call. XML parses into a document tree that you then have to walk — getElementsByTagName, node lists, text nodes, the distinction between an element and its content. For the common case of "give me these five fields", that is a great deal of ceremony for no benefit.
The model matches the data. Most API payloads are records: named fields with values, and lists of those. JSON's objects and arrays are exactly that. XML's elements-plus-attributes model is designed for marked-up text, and using it for records means either choosing arbitrarily between attributes and child elements, or picking one convention and defending it forever.
It was already in the browser. As the web moved to client-rendered applications, the format that needed no library and no traversal API on the client was going to win regardless of its merits. SOAP's decline took a lot of XML tooling with it.
The size difference is real but usually overstated. XML is typically 1.5–2× the bytes of the equivalent JSON, and both compress extremely well because both are repetitive text. Once gzip or Brotli is in the path, verbosity is closer to a readability issue than a bandwidth one.
Four things XML does that JSON cannot
Any comparison that stops at "JSON is better" is not describing the world. These are genuine capabilities with no JSON equivalent.
Validation is part of the specification. The XML recommendation defines two distinct standards: a document is well-formed if it obeys the syntax, and valid if it "has an associated document type declaration and if the document complies with the constraints expressed in it". That distinction is built in, along with validating and non-validating processors. JSON has no equivalent in its specification at all — JSON Schema is a separate standard that you must adopt and enforce yourself, and most projects do neither.
Mixed content. Text with markup inside it — a paragraph containing a link and an emphasised phrase — is what XML was built for and what JSON models worst. Every JSON representation of a rich document ends up either as an HTML string inside a field, which gives up all structure, or as an elaborate node tree that reimplements XML badly.
Namespaces. XML can combine vocabularies from different sources in one document without name collisions. JSON's answer is convention: prefixed keys, and hope. This matters much more than it sounds when documents are assembled from multiple systems.
Transformation as a standard. XSLT transforms one XML document into another declaratively. The JSON equivalent is writing code. For pipelines where the transformation is the product — publishing, financial messaging — that is a real difference.
Add comments to the list, which JSON also lacks, and the pattern is clear: XML's extra machinery is the cost of capabilities that document-oriented work genuinely needs.
Where XML is still the right answer
Not legacy systems — current, deliberate choices:
Documents rather than records. DOCX, ODF and EPUB are XML underneath. So is SVG. When the content is marked-up text, XML is not the compromise.
Regulated industries with mandated schemas. Financial messaging (ISO 20022), healthcare (HL7 CDA), government filings, tax and legal submissions. The schema is the contract, validation is the enforcement, and there is no JSON pathway.
Publishing pipelines. Where XSLT and XPath are doing real work and would need reimplementing as code.
Feeds. RSS and Atom are XML and are not going anywhere.
Configuration in some ecosystems. Maven and older .NET tooling, where the surrounding tooling assumes it.
If you are building a new HTTP API in 2026, JSON is the default and choosing XML needs a reason. If you are exchanging documents with a party who has published a schema, XML is the default and choosing JSON needs their agreement.
Converting between them, and what gets lost
XML to JSON is lossy in a way that surprises people, because the two models do not correspond.
Attributes are the crux. XML distinguishes <user id="42"> from <user><id>42</id></user>; JSON has one kind of member, so a converter must invent a convention. ToolForge's converter prefixes attributes with @ and puts element text under #text, which is a common approach and makes the original distinction recoverable:
{ "user": { "@id": "42", "name": { "#text": "Ada Lovelace" } } }
Four other things do not survive cleanly:
Types. Everything in XML is text, so 42 converts to the string "42" unless the converter guesses — and guessing turns a phone number with a leading zero into a mangled integer. Not guessing is the safer default.
Repeated elements. One <language> child converts to an object; two convert to an array. A consumer that expected an array gets an object when the data happened to have a single item, which is one of the most common bugs in XML-to-JSON pipelines. Handle both shapes.
Namespaces. A prefix like ns2:element becomes part of the key name, so the namespace's meaning is gone even though the text survives.
Comments, CDATA and processing instructions. Dropped entirely; JSON has nowhere to put them.
Going the other way, JSON to XML, is mechanically easier but requires you to invent element names for array items — since JSON arrays are unnamed — which means the round trip does not return the document you started with.
XML to JSON Converter
Parse XML strings to clean JSON objects in your browser. It runs in your browser, so the files never leave your machine, and there is nothing to install or sign up for.
Try the XML to JSON ConverterFrequently asked questions
Is XML obsolete?
No, it has stopped being the default for new APIs. It remains the foundation of document formats, publishing pipelines, feeds and every industry whose data exchange is defined by a mandated schema. Those are large domains where nothing about JSON makes it a better fit.
Which is faster to parse?
JSON, generally and often substantially — the grammar is smaller and the output is native data structures rather than a document tree. The gap narrows for very large documents where a streaming XML parser can process incrementally, while a naive JSON parse wants the whole document in memory first.
Can I convert XML to JSON and back without losing anything?
Not reliably. The attribute-versus-element distinction, namespaces, comments and CDATA have no JSON equivalent, and single versus repeated elements are ambiguous in the JSON direction. Converters use conventions to preserve what they can, but a lossless round trip is not something to design a system around.
Which is better for a public API?
JSON, unless your consumers tell you otherwise. It is what client libraries, documentation tooling and developers expect. If some consumers need XML, content negotiation lets you serve both from one implementation rather than choosing.
The short version
Pick by what your data is. Records moving between services: JSON, and the decision is not close. Documents, mixed content, or an exchange governed by a published schema: XML, and the extra ceremony is what you are paying for. If you are converting XML to JSON, decide up front how you will handle attributes and how you will cope with an element that appears once in one payload and three times in the next — those two questions cause most of the bugs in that pipeline.
Tools that go with this
Each one is here for a specific reason rather than because it is in the same category.
JSON Formatter
Format, validate, and beautify JSON online
Once XML has become JSON, this is what makes the result readable and lets you inspect its structure.
JSON Path Finder
Find and extract values from JSON using JSONPath
Pulls a single value out of a converted document, which is the JSON equivalent of an XPath query.
JSON to CSV Converter
Convert JSON data to CSV format
For the common end of this journey: an XML export that somebody actually wants as a spreadsheet.
YAML ↔ JSON Converter
Bidirectional YAML and JSON conversion with error reporting
The third format in this family, and the one that beat both for configuration files.
Related articles
What Is JSON?
JSON is a text format for structured data, with six value types and a grammar you can read in an afternoon. What it is, where it came from, and where it bites.
JSON vs YAML: What Is the Difference?
YAML is JSON with comments, less punctuation and more ways to shoot yourself. What differs, why config chose YAML, and the traps in converting between them.
JSON Syntax Reference
Every JSON syntax rule in one place: the six value types, the escape sequences, what numbers may contain, and the rules people think exist but do not.
How to Escape Strings in JSON
Escaping JSON is easy until there are layers. How to escape a string, how to nest JSON inside JSON, and how to get a payload through a shell intact.
How to Format JSON
Turn minified JSON into something readable, or the reverse. What formatting changes about your data, what it silently normalises, and when to minify.
How to Validate JSON
Validating JSON answers three different questions: is it well-formed, does it match a schema, and is the data right. How to check each, and where each stops.
Why Your JSON Is Invalid — Every Error Explained
Match the parser error to its cause: unexpected token, unexpected end of input, bad control character, and the invisible BOM. With the fix for each.
Sources
- Extensible Markup Language (XML) 1.0 (Fifth Edition)
W3C · Standards organisation · published 26 November 2008 · accessed 7 September 2026
Cited for: The well-formed versus valid distinction, document type declarations, and validating versus non-validating processors
- RFC 8259 (STD 90) — The JavaScript Object Notation (JSON) Data Interchange Format
IETF · Standards organisation · published 1 December 2017 · accessed 7 September 2026
Cited for: JSON's value types and the absence of any validation or namespace mechanism in the format itself
- ECMA-404, 2nd edition — The JSON Data Interchange Syntax
Ecma International · Standards organisation · published 1 December 2017 · accessed 7 September 2026
Cited for: That JSON defines syntax alone and provides no semantics — the contrast with XML's built-in validity concept
About the author
Builder and maintainer, ToolForge
Started ToolForge in May 2026 and has built and maintained it since, writing every tool on the site and the documentation that goes with each one.
Part of
JSONWhat JSON is, the syntax rules that actually trip people up, why a parser rejected your file, and how JSON compares with XML and YAML.
Browse all developer tools