Guide

JSON vs YAML vs XML: differences and when to use each

Published November 26, 2026 · 5 min read

All three formats describe structured data, but they were designed for different jobs. Here is the same data in each, how they compare, and when to pick which.

The same data in three formats

JSON, YAML and XML can all describe structured data. The easiest way to see how they differ is to write the same thing in each. Here is a small web service configuration.

JSON

{
  "service": "api",
  "port": 8080,
  "debug": false,
  "hosts": ["api1.example.com", "api2.example.com"],
  "database": {
    "engine": "postgres",
    "pool": 10
  }
}

YAML

# Production settings
service: api
port: 8080
debug: false
hosts:
  - api1.example.com
  - api2.example.com
database:
  engine: postgres
  pool: 10

XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Production settings -->
<config service="api">
  <port>8080</port>
  <debug>false</debug>
  <hosts>
    <host>api1.example.com</host>
    <host>api2.example.com</host>
  </hosts>
  <database engine="postgres">
    <pool>10</pool>
  </database>
</config>

YAML is the shortest and has a comment. JSON is compact and unambiguous. XML is the most verbose, but it can attach attributes to elements (service="api") as well as nesting child elements, which the other two can’t express directly.

Side-by-side comparison

JSONYAMLXML
Main useAPIs, data exchangeConfig filesDocuments, enterprise and legacy systems
CommentsNoYes (#)Yes (<!-- -->)
StructureBraces and bracketsIndentationOpening and closing tags
Built-in typesstring, number, boolean, null, object, arraySame as JSON, plus dates and more, depending on schemaEverything is text unless a schema says otherwise
AttributesNoNoYes
Multiple documents per fileNo (but see JSON Lines)Yes (---)No
References / reuseNoAnchors and aliases (&, *)Entities, XInclude
Schema languageJSON SchemaUsually JSON SchemaXSD, DTD, RELAX NG
Whitespace mattersNoYes, indentation is syntaxMostly no
SizeSmallSmallestLargest
Parsing speedFast, simple grammarSlower, complex grammarSlower, especially with validation

JSON: the default for data exchange

JSON has a tiny grammar with six data types (see What is JSON?). That simplicity makes it fast to parse, hard to misinterpret, and supported everywhere, including natively in every web browser. It is the standard choice for REST APIs, webhooks, browser storage and most data sent between programs.

Strengths: unambiguous, fast, universal support, maps directly onto objects and arrays in code.

Weaknesses: no comments, no trailing commas, and lots of quotes and braces, which makes large hand-edited files tiring to maintain. A single missing comma breaks the whole file (see fixing JSON errors).

YAML: the favourite for configuration

YAML (“YAML Ain’t Markup Language”) was designed to be written and read by people. It uses indentation instead of brackets, needs quotes only when a value is ambiguous, and allows comments. That is why it is used by Kubernetes, Docker Compose, GitHub Actions, GitLab CI, Ansible and many other tools for configuration.

Since version 1.2, YAML is designed as a superset of JSON: a valid JSON document is also valid YAML. So you can paste JSON into most YAML files and it will usually work.

Strengths: very readable, comments, multi-line strings, anchors to avoid repetition, several documents in one file.

Weaknesses, which every YAML user eventually meets:

  • Indentation errors. A single wrong space can silently move a key to the wrong level instead of causing an error. Tabs are not allowed for indentation.
  • Implicit typing. Unquoted values are guessed. Under the older YAML 1.1 rules, still used by some popular parsers, no, off and NO become the boolean false. A list of country codes containing NO (Norway) turns into false, which is known as the “Norway problem”. Version strings like 1.10 can become the number 1.1. Quote values when in doubt.
  • Complexity. The full YAML specification is large, and different parsers don’t always agree on edge cases.
  • Unsafe loading. Some YAML libraries can create arbitrary objects from special tags. In Python, use yaml.safe_load(), not yaml.load(), for anything you didn’t write yourself.

XML: structured documents and established standards

XML (eXtensible Markup Language) is older than both. It represents data as a tree of elements with opening and closing tags, and each element can also have attributes. It supports namespaces, so documents can mix vocabularies without name clashes, and it has mature tools for validation (XSD), querying (XPath, XQuery) and transformation (XSLT).

You will meet XML in SOAP web services, RSS and Atom feeds, SVG images, Office files (.docx and .xlsx are zipped XML), Android layouts, Maven pom.xml, sitemaps, and many banking, healthcare and government standards.

Strengths: mixed content (text with markup inside, like HTML), attributes, namespaces, strong validation and tooling.

Weaknesses: verbose, slower to parse, and it doesn’t map neatly onto objects and arrays: there is no built-in difference between a single item and a list, and you must decide whether each value is an attribute or a child element. XML parsers should also be configured to disable external entities, to prevent XXE (XML external entity) attacks.

Which one should you use?

  • Building or calling an API? Use JSON. It is what clients expect, and every language handles it well.
  • Writing configuration that people edit by hand? YAML is the most pleasant to read and supports comments. If your tool expects JSON, a JSON variant with comments, or TOML for simpler flat settings, are also good options.
  • Working with documents, feeds or an existing standard? Use XML when the ecosystem requires it, such as RSS, SVG, SOAP or industry formats, or when you need mixed text and markup.
  • Storing logs or streaming records? Use JSON Lines: one JSON object per line, easy to append and process line by line.

Converting between them

Because JSON and YAML share the same data model, converting between them is lossless, apart from YAML comments, which JSON can’t hold. Use the JSON ↔ YAML converter to switch a Kubernetes manifest or Compose file in either direction.

Converting XML to JSON needs decisions, because XML has attributes, mixed content and repeated elements that JSON represents differently. Different converters make different choices, for example prefixing attributes with @ or always using arrays for repeated elements, so check the result matches what your code expects.

Summary

  • JSON: simple, strict and universal. The best choice for data exchanged between programs.
  • YAML: readable and supports comments. Great for configuration, but quote ambiguous values and watch indentation.
  • XML: verbose but powerful. Use it for documents and the many standards built on it.