Skip to content
XUtilHub

JSON vs XML: Differences, Examples and When to Use Each

XUtilHub Team ·

JSON and XML both store structured data as text, and both are still everywhere. JSON dominates web APIs, while XML remains strong in documents, enterprise systems and configuration. Here's how they compare.

The same data in both

JSON:

{
  "book": {
    "title": "Clean Code",
    "author": "Robert C. Martin",
    "year": 2008,
    "tags": ["programming", "best practices"]
  }
}

XML:

<book>
  <title>Clean Code</title>
  <author>Robert C. Martin</author>
  <year>2008</year>
  <tags>
    <tag>programming</tag>
    <tag>best practices</tag>
  </tags>
</book>

Key differences

JSONXML
SyntaxBraces, brackets, key–value pairsOpening and closing tags
SizeSmallerLarger (every tag is written twice)
Data typesString, number, boolean, null, array, objectEverything is text unless a schema says otherwise
ArraysBuilt inRepeated elements
AttributesNoYes
CommentsNoYes
SchemasJSON SchemaXSD, DTD
QueryingJSONPathXPath, XQuery
NamespacesNoYes

Why JSON won the web

  • It maps directly to objects in JavaScript, Python and most languages.
  • It's shorter and easier to read.
  • Parsing is fast and simple.
  • Numbers, booleans and null have real types.

Almost every modern REST API returns JSON.

Where XML still shines

  • Documents with mixed content, where text and markup interleave, as in HTML, DOCX and SVG, all XML-based.
  • Strict validation with XSD schemas, common in banking, government and healthcare standards.
  • Feeds and sitemaps: RSS, Atom and sitemap.xml.
  • Enterprise integrations built on SOAP.

Converting between them

The conversion isn't always one-to-one. XML attributes, namespaces and mixed text don't have direct JSON equivalents, and JSON arrays become repeated XML elements. Converters follow conventions, for example turning attributes into keys prefixed with @.

Use JSON to XML to convert, the XML formatter to tidy and validate XML, and the JSON formatter to make JSON readable.

Which should you choose?

For a new web API or app configuration, choose JSON. Use XML when a standard or partner system requires it, or when you're marking up documents rather than exchanging data.

Tools mentioned in this article

← All articles