XML Tutorial · XML in the Real World · Lesson 27 of 28

XML vs JSON vs YAML

How XML, JSON and YAML compare, how to convert between XML and JSON without losing information, and how to choose a format.

Create a free account to save this page, take notes and track your progress. Sign in

Side by side

<order id="1042"><customer>Ada</customer><item qty="2">Tea</item><item qty="1">Mug</item></order>
{"id": 1042, "customer": "Ada", "items": [{"name": "Tea", "qty": 2}, {"name": "Mug", "qty": 1}]}
id: 1042
customer: Ada
items:
  - {name: Tea, qty: 2}
  - {name: Mug, qty: 1}
XML JSON YAML
Data types Text (types come from a schema) String, number, boolean, null, object, array Like JSON, plus dates and more
Comments Yes No Yes
Attributes vs children Both Only properties Only keys
Mixed content (documents) Natural Awkward Awkward
Schema and validation DTD, XSD, RELAX NG, Schematron JSON Schema JSON Schema (usually)
Query and transform XPath, XSLT, XQuery JSONPath, jq Converted to JSON first
Namespaces Yes No No
Size and verbosity Largest Small Smallest to type
Typical use Documents, standards-based exchange, enterprise, Office/SVG Web APIs, configuration Configuration (Kubernetes, CI)

Converting XML to JSON

There is no single correct mapping: you must decide how attributes, repeated elements, text and types become JSON. Write the mapping explicitly for your data:

XML to JSON with an explicit mappingTry it yourself
import json
from lxml import etree

order = etree.fromstring(b'<order id="1042"><customer>Ada</customer><item qty="2">Tea</item><item qty="1">Mug</item></order>')
data = {
    "id": int(order.get("id")),
    "customer": order.findtext("customer"),
    "items": [{"name": i.text, "qty": int(i.get("qty"))} for i in order.findall("item")],
}
print(json.dumps(data, separators=(",", ":")))

Output

{"id":1042,"customer":"Ada","items":[{"name":"Tea","qty":2},{"name":"Mug","qty":1}]}

In real Java code use Jackson (jackson-dataformat-xml) instead of building JSON by hand; the hand-written version above only keeps the example free of dependencies.

Generic converters (Python's xmltodict, or PHP's json_encode(simplexml_load_string(...))) are quick but lossy and inconsistent: one <item> becomes an object while two become an array, and every value is a string.

Choosing

  • Public web or mobile API: JSON.
  • Configuration edited by people: YAML or TOML (JSON if it's generated).
  • Documents with markup, standards that mandate XML (finance, healthcare, government, publishing), strict schema-validated contracts, or XSLT pipelines: XML.
  • Don't convert working XML integrations to JSON for fashion; do validate whatever you accept.

Test yourself

Which is a reason to prefer XML over JSON?

  1. Documents with mixed content and markup
  2. Smaller payloads
  3. Native number types
  4. Browser APIs return JSON
Show answer

Answer: Documents with mixed content and markup

XML handles text interleaved with markup naturally; JSON is better for plain data structures.

Was this page helpful?