JSON vs XML vs YAML: Choosing the Right Data Format
JSON, XML, and YAML can all describe the same data, whether a user record, a config file, or an API payload, but each optimizes for different things. Pick the wrong one and you pay later in unreadable config diffs or a parser that can't express what you need. This guide compares the three on syntax, size, speed, and features, with a rule of thumb for each.
The same data, three ways
Below, one small object appears in all three formats. Comparing them side by side shows what each format trades off.
{
"user": {
"name": "Ada Lovelace",
"roles": ["admin", "editor"],
"active": true
}
}user:
name: Ada Lovelace
roles:
- admin
- editor
active: true<user active="true">
<name>Ada Lovelace</name>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
</user>The same three fields take three very different amounts of punctuation. JSON suits machines best, YAML is the easiest to write by hand, and XML gives you the most structure when the document needs more than key/value data.
Where JSON wins
JSON maps onto the data structures every mainstream language already has (objects/dicts and arrays/lists), so parsing round-trips into native types with zero ambiguity. It has no comments, one way to write each value, and no whitespace sensitivity. Those limits make it fast to parse and safe to generate from code.
Use JSON for anything machine-to-machine: REST and GraphQL API payloads, data passed between services, anything stored in a document database (MongoDB, PostgreSQL's JSONB), and any config that code reads and writes more than people edit.
Where YAML wins
YAML is a strict superset of JSON's data model with a friendlier syntax for humans: no closing brackets to count, comments with #, and indentation in place of punctuation. You want that for files a person edits and re-reads in a diff: CI pipelines (GitHub Actions, GitLab CI), Kubernetes manifests, and application config.
That flexibility is also YAML's biggest footgun. An indentation error produces a different structure with no parse error, and the parser can convert unquoted values like `no`, `yes`, `on`, and dates to booleans or timestamps you didn't intend (the infamous 'Norway problem', where the country code NO becomes the boolean false). Quote your strings whenever a value could be read two ways.
Where XML wins
XML looks like legacy overhead next to JSON, but it handles things JSON and YAML can't: attributes alongside element content, mixed content (text and elements interleaved, like HTML), namespaces for combining vocabularies from different sources, and mature schema validation (XSD) plus transformation (XSLT) tooling that predates JSON by a decade.
XML persists in document-centric domains: SOAP APIs, SVG, DOCX/XLSX (ZIP files full of XML), and enterprise systems in banking, healthcare, and government with XSD-validated pipelines nobody plans to rewrite. If your data is document-shaped ('a paragraph with a bolded span in the middle') instead of record-shaped, XML often fits better.
Size and parsing speed
For the same record-shaped data, YAML is the smallest on disk, JSON lands within a few percent of it, and XML runs roughly 30–40% larger than JSON once you count closing tags and attributes or wrapper elements. A 1 KB JSON payload usually becomes a 950-byte YAML file and a 1.3–1.4 KB XML document. Those gaps matter for files on disk, payloads embedded in bundles, and anything stored uncompressed. Over HTTP they matter much less.
Compression explains why. Nearly every production API and CDN enables gzip or brotli, which squash repeated punctuation and indentation, and all three formats compress well. After brotli, the minified-JSON and pretty-YAML versions of the same data usually land within a few percent of each other. Parsing tells a similar story: JSON.parse is a native, heavily optimized code path in every browser and in Node, YAML needs a library and a far heavier grammar, and XML sits in between. You won't measure any of it for config parsed once at startup, but on a hot path JSON's cheap parser helps.
Comments, multi-document files, and other sharp edges
Comments: YAML has them (#), XML has them (<!-- -->), and JSON leaves them out on purpose. If two documents could differ only by a comment, implementations that strip or keep comments would disagree about equality, which breaks signatures, hashes, and cache keys.
Multi-document files: YAML separates documents with ---, so one file holds a stream of records. XML permits one root element. JSON defines a single value per document, and the ecosystem uses JSON Lines (one minified document per line) for streams. YAML also has anchors and aliases (&anchor / *alias) for reuse, which JSON has no equivalent for.
Each format has a characteristic sharp edge. With YAML's indentation sensitivity and implicit type coercion (the Norway problem), a file can parse into a different structure than you intended. XML's entity expansion opens an attack surface (the XXE vulnerability class) in misconfigured parsers, and namespaces add verbosity JSON never has. JSON's edges are milder: no comments, no date type, integer precision capped at 2^53. That makes it the safest of the three to accept from untrusted sources.
Converting between formats
All three describe the same object/array/string/number/boolean/null data model (XML's attributes and mixed content don't map cleanly), so converting record-shaped data between JSON, YAML, and XML is mechanical. JsonForge's JSON to YAML, YAML to JSON, JSON to XML, and XML to JSON tools convert in the browser, which helps when you migrate a config file's format or hand a YAML-only teammate a JSON export.
XML is the awkward direction. JSON has no distinction between attributes and child elements, so each converter adopts a convention (an @ prefix for attributes, or a fixed sub-object), and a document round-tripped through it comes back in a different shape. JSON ⇄ YAML conversion loses nothing in either direction.
FAQ
- Is YAML JSON with different syntax?
- Almost. YAML 1.2 is a superset of JSON, so any valid JSON document is also valid YAML. YAML adds features JSON lacks (comments, anchors/references, multi-document files) and its own parsing quirks (implicit type conversion), so the reverse fails: plenty of YAML isn't valid JSON.
- Why do Kubernetes and CI tools use YAML instead of JSON?
- People write and review those files by hand all the time. YAML's lack of brackets and support for comments make it far more pleasant to read and diff in a pull request than the equivalent JSON, even though JSON would parse to the same data.
- Why does XML still exist if JSON is simpler?
- XML supports things JSON can't express: attributes on elements, mixed text/element content, namespaces, and schema validation via XSD. Formats built on XML (SVG, DOCX, XLSX, SOAP) rely on those features, so XML stays around even though JSON won for typical REST APIs.
- What is the YAML 'Norway problem'?
- In YAML, the parser reads unquoted `NO` (Norway's country code) as the boolean `false`, because YAML converts unquoted `no`/`yes`/`on`/`off` to booleans. Quote country codes, version strings, and anything that looks like a boolean or number but should stay a literal string.
- Why doesn't JSON support comments?
- Comments would break the guarantee that equal data has one serialized form. Machines generate and compare JSON, and with comments two parsers could disagree about whether two documents match, which breaks signing, hashing, and caching. JSONC and JSON5 exist for hand-edited config, but they're supersets of JSON.
Try these tools
JSON to YAML →
Convert JSON to YAML and back, with type inference and clean formatting.
YAML to JSON →
Convert YAML configs — CI pipelines, Kubernetes manifests, OpenAPI specs — into JSON, with precise parse errors.
JSON to XML →
Convert JSON to XML and back, with automatic root-element wrapping.
XML to JSON →
Convert XML — SOAP responses, RSS feeds, legacy exports — into JSON, keeping attributes and reporting parse errors.
Related articles
What is JSON? A Complete Beginner's Guide →
JSON (JavaScript Object Notation) is the lightweight data-interchange format behind nearly every API, config file, and NoSQL database. Learn the syntax, the six data types, how parsing behaves in code, and the mistakes everyone makes hand-writing it.
The History of JSON: From 2000 to Industry Standard →
Trace JSON from Douglas Crockford's idea in 2001, through Yahoo's adoption, to becoming the ECMA-404 standard that powers 90% of modern APIs.
The Complete JSON Schema Guide (Draft 7) →
JSON Schema is the standard for describing and validating JSON structure. Learn the core keywords, build a real API schema, compose and reuse schemas with $ref, and run validation in code with Ajv.