The short answer
| If you're writing… | Use | Why |
|---|---|---|
| An API response or request body | JSON | Every language parses it the same way, and browsers have it built in. |
| A config file people edit by hand | YAML (or JSON with comments, if your tool allows it) | Comments, no brackets to balance, easy to diff. |
| Documents with mixed text and markup, or a format someone else defined | XML | Attributes, namespaces, schemas, and it's what SOAP, RSS, SVG and Office files use. |
The same data three ways
A small user record, first as JSON:
{
"name": "Aung",
"city": "Bangkok",
"roles": ["editor", "admin"],
"active": true
}
As YAML, with a comment, which JSON can't have:
# staff list, edited by hand name: Aung city: Bangkok roles: - editor - admin active: true
And as XML, where you have to decide what's an element and what's an attribute:
<user active="true"> <name>Aung</name> <city>Bangkok</city> <roles><role>editor</role><role>admin</role></roles> </user>
XML has no built-in idea of a list, a number or a true/false value. Everything is text until a schema or your code says otherwise, which is why converting XML to JSON always involves a few choices. The XML to JSON converter shows you how it made them.
JSON: strict, and that's the point
JSON's rules are short and leave nothing open: double quotes only, no trailing commas, no comments, and only six kinds of value (string, number, true/false, null, object, array). That strictness is why a JSON file means exactly the same thing in JavaScript, Python, Go or a database.
Two things JSON parsers do differently, both worth knowing:
- Big numbers lose precision in JavaScript.
JSON.parse('{"id": 12345678901234567890}')in Node.js gives12345678901234567000, because JavaScript numbers can only hold whole numbers exactly up to 9,007,199,254,740,991. Send large IDs as strings. - Duplicate keys are allowed, and the last one wins.
{"a":1,"a":2}parses to{"a":2}in both JavaScript and Python, with no warning.
Python is also more relaxed than the standard in one place: json.loads('{"score": NaN}') works in Python, while
JSON.parse in JavaScript rejects it. If a Python service sends NaN, a browser can't read the response.
When JSON won't parse, the JSON errors guide explains each message and the
JSON formatter shows the exact line.
YAML: friendly to write, surprising to read
YAML is a superset of JSON (any valid JSON is valid YAML), with comments, multi-line strings and no brackets. Its weak spot is that it guesses types from unquoted text, and different libraries guess differently. Here's one file:
country: no enabled: on version: 1.10 mode: 0755 zip: 01234 time: 12:30
And what two popular parsers actually return for it:
| Line | PyYAML 6 (Python) | js-yaml 4 (JavaScript) |
|---|---|---|
country: no | false | "no" |
enabled: on | true | "on" |
version: 1.10 | 1.1 | 1.1 |
mode: 0755 | 493 (read as octal) | 755 |
zip: 01234 | 668 (octal again) | 1234 |
time: 12:30 | 750 (minutes, base 60) | "12:30" |
PyYAML follows the older YAML 1.1 rules, where no, off and on are booleans and numbers
with a leading zero are octal. js-yaml follows YAML 1.2. The country code for Norway is NO, which is why this is known
as “the Norway problem”. Both parsers agree that 1.10 is the number 1.1, which is never what you meant for a version.
The fix is simple: quote anything that isn't obviously a number or a boolean. country: "no",
version: "1.10" and zip: "01234" come out as text in every parser. The
YAML validator shows the parsed result, so you can check what your file really says.
The other classic YAML mistake is indentation: tabs aren't allowed at all, and one space too many moves a key into the wrong parent without any error.
XML: verbose, but precise
XML is heavier to write, but it does things the other two can't:
- Mixed content: text with markup inside it, like
<p>Hello <b>world</b></p>. - Attributes and namespaces, so two vocabularies can share one document (SVG inside XHTML, for example).
- Schemas (XSD) that many enterprise and government systems use to validate data before accepting it.
You'll rarely choose XML for something new, but you'll meet it in RSS feeds, sitemaps, SOAP APIs, Android layouts, Maven
pom.xml files and inside every .docx and .xlsx file.
Side by side
| Feature | JSON | YAML | XML |
|---|---|---|---|
| Comments | No | Yes (#) | Yes (<!-- -->) |
| Data types | 6 built in | Guessed from text | All text, unless a schema says otherwise |
| Parses the same everywhere | Almost always | No: depends on the library and YAML version | Yes, but structure is up to you |
| Easy to edit by hand | OK, but brackets and commas | Yes, until indentation goes wrong | Tiring |
| Typical use | APIs, data exchange, package.json | Docker Compose, Kubernetes, GitHub Actions, CI config | Documents, feeds, SOAP, Office files |
Converting between them
JSON to YAML is safe: every JSON value has an exact YAML equivalent, and the JSON to YAML converter quotes strings that would otherwise be misread. YAML to JSON is where the gotchas above show up, so check the result with the YAML to JSON converter. XML to JSON always needs decisions about attributes, repeated elements and text, so look at the output before you rely on it.
How this guide was checked: every example on this page was run as written, using PyYAML 6.0.3 and js-yaml 4.1.0 (the YAML library the curlybits tools use), Node.js 22. Spotted a mistake? Tell us and we'll fix it.
Updated