Compare XML and JSON data formats
XML file vs JSON: how each format structures data, where each fits in APIs and configuration, and what changes or breaks when you convert between them.
XML and JSON both store structured data as plain text, so either one can be opened in a text editor, sent over HTTP or kept in version control. They model data differently, though, and those differences decide which one suits a task and what happens when you convert one into the other. This guide compares the two formats, explains what a conversion actually changes, and covers the checks that keep the output valid where you plan to use it.
What each format represents
XML (Extensible Markup Language) is a W3C markup standard. Data lives in nested elements marked by opening and closing tags. Elements can carry attributes, and a document can include namespaces, comments, processing instructions and mixed content, which means text and child elements interleaved inside a single element. Without a schema, every value is text. Types and structure rules come from separate schema languages such as DTD or XML Schema (XSD).
JSON (JavaScript Object Notation) is defined by RFC 8259 and ECMA-404. It has a small fixed set of value types: objects (name–value pairs), arrays, strings, numbers, booleans and null. It has no attributes, namespaces or comments. JSON Schema can describe and validate structure, but it is a separate specification, as XSD is for XML.
Here is the same order written both ways. The JSON is one reasonable hand-written mapping, not the only correct one.
<order id="1042" currency="USD">
<customer>Acme Supplies</customer>
<item sku="A-7" qty="2">Notebook</item>
<item sku="B-3" qty="1">Pen</item>
</order>
{
"order": {
"id": 1042,
"currency": "USD",
"customer": "Acme Supplies",
"items": [
{ "sku": "A-7", "qty": 2, "name": "Notebook" },
{ "sku": "B-3", "qty": 1, "name": "Pen" }
]
}
}
What changes when you convert XML to JSON (or back)
A conversion between XML and JSON changes the data model, not just the syntax. No single standard governs the mapping, so different converters and libraries make different choices. A round trip from XML to JSON and back to XML can therefore produce a document that differs from the original unless both directions follow the same documented convention. These are the points where meaning is most often altered or lost:
- Attributes vs. child elements: JSON has no attributes, so converters either merge them into ordinary keys or mark them with a prefix such as @. After merging, you can no longer tell which values were attributes.
- Repeated elements: Two item elements become an array, but a single item may become a plain object. The shape of the JSON then depends on how many items happened to be present.
- Data types: XML text such as 2 or true stays a string unless the converter guesses a type or applies a schema. Guessing can turn identifiers like 007 into the number 7.
- Order: XML element order is significant. RFC 8259 treats object member order as not significant, so order that matters must be kept in an array.
- Mixed content: Text interleaved with markup, as in a paragraph containing emphasized words, has no natural JSON equivalent.
- Namespaces, comments and processing instructions: These are usually dropped or folded into awkward key names.
- Whitespace: Whitespace between JSON tokens carries no meaning. In XML, whitespace inside text content can matter, especially in mixed content.
The following shows how a generic attribute-prefix convention might render the same order. It is illustrative, not output observed from any particular tool. Every value is a string, and the item array would collapse to a single object if the order contained only one item.
{
"order": {
"@id": "1042",
"@currency": "USD",
"customer": "Acme Supplies",
"item": [
{ "@sku": "A-7", "@qty": "2", "#text": "Notebook" },
{ "@sku": "B-3", "@qty": "1", "#text": "Pen" }
]
}
}
Converting JSON to XML raises the opposite problems. JSON keys can contain characters that are not legal in XML element names, such as spaces or a leading digit. Arrays need a wrapper or repeated element name. Null, booleans and numbers have to be encoded as text, with a schema or naming convention if the receiver needs to recover their types.
Which format suits your intended result
| Criterion | XML | JSON |
| Core model | Elements, attributes, text, namespaces | Objects, arrays, strings, numbers, booleans, null |
| Built-in value types | Text only, unless a schema assigns types | Small native set of types |
| Comments | Supported | Not part of the standard |
| Document-style and mixed content | Handled natively | No natural representation |
| Namespaces for combining vocabularies | Supported | No built-in mechanism |
| Validation | DTD, XML Schema and others | JSON Schema |
| Querying and transformation | XPath, XQuery, XSLT | Library- and tool-specific query languages |
| Verbosity | Closing tags repeat element names | Usually terser for the same records |
| Hand editing | Readable but more verbose | Readable; strict syntax (no trailing commas, double quotes only) |
| Round-trip conversion | Not guaranteed without an agreed mapping | Not guaranteed without an agreed mapping |
Choose XML when the data is document-like, when you need namespaces to combine vocabularies, when comments or mixed content must be preserved, or when an existing standard, partner or toolchain already specifies XML with a schema. Choose JSON when the data consists of records, lists and simple typed values, especially when it is consumed by JavaScript or by the JSON support built into most modern languages.
The verbosity row describes structure only. The supplied material contains no measurements of file size, parsing speed or memory use, so this guide makes no performance claims. If performance matters, benchmark your own payloads with the parsers you actually use.
XML vs JSON in APIs
For APIs, the practical question is usually what the other side expects. Many current web APIs exchange JSON, while SOAP-based services are defined around XML envelopes, and some services offer both formats through content negotiation using the Accept and Content-Type headers. When you integrate with an existing API, follow its documented format and schema rather than converting on the fly, because the conversion issues above can quietly change types or array shapes that the receiving code depends on. When you design a new API, pick the format your consumers and tooling handle best, and publish a schema whichever you choose.
Will the output stay valid and usable where you need it?
Both formats are plain text, so formatted or converted output survives copy and paste as long as the characters stay unchanged. Problems usually come from the route the text takes, not from the format itself:
- Word processors, chat apps and some rich-text editors can replace straight quotes with curly quotes, which breaks both JSON and XML. Paste into a plain-text or code editor instead.
- JSON parsers that follow the standard reject comments, trailing commas and single-quoted strings, even though some lenient tools accept them. Check what your destination parser allows.
- RFC 8259 requires UTF-8 for JSON exchanged between systems that are not part of a closed ecosystem. XML can declare an encoding in its prologue, and that declaration must match the actual bytes. A byte order mark or a mismatched encoding can make a file fail to parse.
- Pretty-printing changes only insignificant whitespace in JSON. Re-indenting XML can alter text content where whitespace is meaningful, so avoid reformatting XML that contains mixed content unless the consumer ignores that whitespace.
- RFC 8259 says the behavior of duplicate object keys is unpredictable across implementations, and it notes that numbers beyond the precision of common double-precision implementations may not interoperate. Avoid both in data meant for other systems.
Before relying on the output, validate it in the destination itself or against its schema. Use the parser, schema or API sandbox the data will actually meet, not just the formatter that produced it. A document that one tool shows as well-formed can still fail a stricter consumer.
Parsing untrusted XML carries specific risks. External entity references (XXE) can make a parser read local files or contact other systems, and recursive entity expansion can exhaust memory. Disable DTD and external entity processing in your XML parser unless you need it, and use the secure-processing options your library documents. JSON has no entity mechanism, but very large or deeply nested input can still strain a parser, so apply size and depth limits to untrusted payloads.
Online formatters and converters receive whatever you paste into them. Before submitting API responses, configuration files or logs to an external service, remove credentials, tokens and personal data. For sensitive content, prefer a local editor extension, a command-line tool or your language's standard library. Unless a service documents otherwise, do not assume pasted text stays private or is deleted.
Gaps in this comparison
No tool-specific test results, benchmarks or version details were available for this guide. The converted JSON examples are illustrative, not observed output, and statements about converters describe common conventions, not the behavior of any named product. Accessibility of either format depends on the editor or viewer that displays it, not on the format, so check that the tool you use works with your screen reader or other assistive technology.