JSON vs XML: Differences, Examples and When to Use Each
JSON is smaller and maps directly to objects in code; XML supports attributes, namespaces, mixed content and mature schema tooling. See the same data in both formats and when each one fits.
Convert XML to JSONThe short answer
Use JSON for web and mobile APIs, browser apps and most configuration: it is compact, every language parses it into native objects and arrays, and it is the default for REST. Use XML when a standard or partner requires it, or when documents need attributes, namespaces, mixed text and markup, or validation with XSD and transformation with XSLT. SOAP, RSS, SVG, Office files and many banking and healthcare formats are XML.
The same data in both formats
JSON has types: strings, numbers, booleans, null, objects and arrays. XML has only text; types come from a schema or from the code reading it. A list in JSON is an array, while XML repeats an element.
{
"book": {
"id": "bk101",
"title": "Learning JSON",
"price": 29.99,
"available": true,
"authors": ["Ada Lovelace", "Alan Turing"]
}
}
<book id="bk101">
<title>Learning JSON</title>
<price>29.99</price>
<available>true</available>
<authors>
<author>Ada Lovelace</author>
<author>Alan Turing</author>
</authors>
</book>Key differences
Most differences follow from JSON being a data format and XML being a markup language.
- Size: JSON has no closing tags, so the same data is usually noticeably smaller before compression.
- Data types: JSON distinguishes 29.99, "29.99", true and null. In XML everything is text until a schema says otherwise.
- Arrays: native in JSON. In XML a single repeated element is ambiguous when converting: one <author> may become a string or a one-item list.
- Attributes and namespaces: XML has both. JSON has neither, so converters invent conventions such as @id for attributes.
- Comments: XML allows <!-- comments -->. Strict JSON does not.
- Mixed content: XML can hold text with inline markup, like a paragraph with <b>bold</b> words. JSON cannot represent that naturally.
- Schemas and tooling: XML has XSD, XPath, XSLT and decades of enterprise tools. JSON has JSON Schema and JSONPath, which are simpler and newer.
- Parsing: JSON.parse in the browser is built in. XML needs a DOM or SAX parser, and parsers must be configured to block XXE (external entity) attacks.
When to choose each
Choose JSON for new APIs, frontend data, NoSQL documents, logs and app settings. Choose XML when you integrate with SOAP services, publish feeds or sitemaps, exchange documents under an industry standard, or need document-style content with markup.
Converting is common at system boundaries. Expect to make decisions about attributes, repeated elements and types; check a sample of the converted output rather than assuming a perfect round trip.
Put the example to work
Use your own sample, check the result, and copy or download it. The tool processes your data locally in your browser.
Convert XML to JSONReference documentation