JSON vs XML: what’s the difference, and when should you use each?

Guide · Updated July 2026 · ~8 min read

JSON and XML solve the same problem — representing structured data as text — with very different philosophies. JSON is a minimal notation for data structures; XML is a full markup language for documents. Knowing where each shines saves you from fighting your format later.

The same data, side by side

JSON                                XML

{                                   <user id="17">
  "id": 17,                           <name>Ana Ivanova</name>
  "name": "Ana Ivanova",              <active>true</active>
  "active": true,                     <roles>
  "roles": ["admin", "editor"]          <role>admin</role>
}                                        <role>editor</role>
                                      </roles>
                                    </user>

The JSON version is shorter and maps directly onto the arrays, objects, numbers, and booleans of every programming language. The XML version is more verbose but carries extra expressive machinery: attributes (id="17"), a named root element, and — not shown here — namespaces, comments, and mixed text-and-markup content.

The key differences

JSONXML
Designed forData interchange between programsStructured documents and markup
Data typesString, number, boolean, null, object, array — built inEverything is text; types come from a schema layer (XSD)
ArraysNative: [1, 2, 3]By convention: repeated elements
Attributes vs valuesNo distinction — everything is a key/valueAttributes and child elements are different things
CommentsNot allowed<!-- allowed anywhere -->
NamespacesNoneBuilt in — mix vocabularies in one document
Schema validationJSON Schema (newer, widely used)DTD, XSD, RELAX NG (mature, very strict)
Typical sizeSmaller — no closing tagsRoughly 1.5–2× larger for the same data
Parsing in JSJSON.parse() — one lineDOMParser — more ceremony

Where JSON wins

Where XML wins

Size and speed, in practice

XML’s verbosity is real but frequently overstated, because the thing that makes it verbose — repeating every element name in the closing tag — is also the thing compression algorithms handle best. For a typical record-shaped payload:

JSONXML
Raw, minified1.0×≈1.5–2.0×
After gzip1.0×≈1.1–1.3×
Pretty-printed overhead+15–25%+20–30%

Since essentially every HTTP server compresses responses, the practical difference on the wire is far smaller than the raw numbers suggest. Where JSON’s advantage stays decisive is parsing in JavaScript: JSON.parse() is implemented in native code and is dramatically faster than building a DOM from XML. If you are choosing a format for a browser-facing API, that — not byte count — is the argument that matters.

Neither one: the rest of the landscape

JSON and XML are not the only options, and sometimes the reason you are unhappy with both is that the job belongs to a third format:

A useful rule: pick the format by who reads it. Humans editing by hand favour YAML or TOML, programs exchanging data over HTTP favour JSON, documents with markup need XML, and machines talking to machines at volume want something binary.

Common misconceptions

“XML is obsolete.” It has lost the web-API war, but it remains everywhere: every RSS feed, every SVG image, every Android app resource, most enterprise message buses. You will read and debug XML for decades to come.

“JSON is always smaller and faster.” Usually, but not inherently — gzip compresses XML’s repetitive tags very well, and a SAX-style streaming XML parser can beat loading a giant JSON blob into memory. For typical API payloads, though, JSON is leaner.

“You can convert losslessly between them.” Only approximately. XML attributes, namespaces, comments, and mixed content have no JSON equivalent, and JSON’s types are lost when everything becomes XML text. Our guide toJSON ⇄ XML conversion covers the pitfalls in detail.

The bottom line

Choose JSON for APIs, configs, and program-to-program data — it’s smaller, simpler, and native to the web. Choose XML for document markup, mixed content, or when an existing standard or schema-validated contract demands it. Most working developers deal with both every week — which is exactly why Indentio’sJSON and XML tools live in the same editor, with a converter between them.

Keep reading