JSON vs YAML vs XML: differences and what conversion loses
JSON, YAML and XML have different data models, so converting between them is a mapping with rules, not a re-spelling. Comments, anchors, attributes, array shape and value types are the things that most often do not survive.
ConvertBoxPublished
Three data models, not three spellings of one
JSON, YAML and XML are often described as interchangeable ways to write the same data. They are not: each has its own data model, and a converter has to invent a rule wherever the models disagree. JSON has exactly six kinds of value. YAML 1.2 can express everything JSON can, and was designed so that a JSON document is also valid YAML, then adds comments, references and several ways to write a string. XML is a document format: a tree of named elements carrying attributes and text, with no built-in notion of number, boolean or array.
| JSON | YAML | XML | |
|---|---|---|---|
| Value types | String, number, boolean, null, array, object | The JSON types, inferred from how an unquoted value is spelled | Text only; types come from a schema or from the reader |
| Comments | None | # to the end of the line | <!-- ... --> |
| Attributes | None | None | Yes: <user id="7"> |
| Reusing a block | None | Anchors and aliases: &base, *base | Only through DTD entities |
| Text mixed with markup | No | No | Yes: <p>Hello <b>world</b></p> |
| Syntax rules | Double quotes only, no trailing commas | Indentation is structure; tabs may not indent | Every tag closed, exactly one root element |
How strict each parser is, and where mistakes hide
JSON (RFC 8259) is the strictest of the three to write: keys and strings need double quotes, a trailing comma is an error, and comments do not exist. The benefit is that a document either parses or fails loudly. When the site's JSON formatter shows an error instead of output, the cause is a real defect, usually a trailing comma, single quotes, a comment, or a value such as NaN pasted from a JavaScript console.
XML is equally unforgiving about well-formedness: every tag closed, attribute values quoted, exactly one root element. On top of that, a schema language such as XSD can check that the right elements appear in the right order with the right types, which neither JSON nor YAML has built in. YAML is the opposite case. It is easy to write, but structure comes from indentation, so a line indented two spaces too far is still valid YAML that means something else, and nothing warns you. Converting a YAML file to JSON is a practical way to see what the parser actually understood, because braces and brackets make the nesting explicit.
Where each format is the normal choice
- JSON for machine-to-machine exchange: web APIs, browser storage, log records, package manifests. Every mainstream language parses it natively and its type set matches what programs use.
- YAML for files people edit by hand: Kubernetes manifests, Docker Compose files, CI pipeline definitions, Ansible playbooks. Comments and anchors keep long configuration readable.
- XML for documents and schema-governed exchange: SOAP services, RSS and Atom feeds, SVG, the parts inside .docx and .xlsx files. Use it when text and markup are mixed or a recipient validates against an XSD.
- A rule of thumb: if a program writes it and a program reads it, use JSON; if a person maintains it, YAML; if a standard or a partner dictates the element names, XML.
YAML type coercion and the Norway problem
YAML decides the type of an unquoted value by its spelling, and the rules changed between versions. YAML 1.1 treats yes, no, on, off and their capitalised forms as booleans, so a list of country codes containing NO, for Norway, comes back with false in it. YAML 1.2 kept only true and false, but widely used parsers, PyYAML among them, still follow 1.1. The same file can therefore mean different things to two tools.
The defence is the same everywhere: quote any string that could be read as another type. The site's JSON to YAML converter does this on output, single-quoting strings such as '012', 'true' and '2024-01-01' as well as the YAML 1.1 words yes, no, on and off, so the result survives older parsers.
| Unquoted YAML | YAML 1.1 parser (such as PyYAML) | ConvertBox YAML to JSON (js-yaml, YAML 1.2 rules) |
|---|---|---|
| country: NO | false | "NO" |
| enabled: yes | true | "yes" |
| zip: 012 | 10 (read as octal) | 12 |
| version: 1.20 | 1.2 | 1.2 |
| released: 2024-01-01 | A date value | "2024-01-01T00:00:00.000Z" |
JSON and YAML: what a round trip drops
JSON to YAML loses no data, because every JSON value has a YAML form. What can change is incidental: the converters here read the input with the browser's JSON parser first, so 1.0 is written as 1, integers above 2^53 (9007199254740991) lose their last digits, duplicate keys collapse to the last one, and keys that look like integers move to the front of their object.
YAML to JSON is where information disappears. Comments are dropped. An anchor (&base) and its aliases (*base) are expanded into full copies, and merge keys (<<: *base) are merged in, so the JSON is correct but the fact that five services shared one block is gone. Block scalars become ordinary strings, non-string keys become strings, and .inf and .nan become null because JSON has no such numbers. The YAML to JSON tool also refuses a stream of several documents separated by --- and application tags such as CloudFormation's !Ref, so convert one document at a time. Treat the YAML file as the source of truth: converting to JSON and back yields equivalent data, never the original file.
JSON and XML: attributes, repeated elements and types
XML has no arrays and JSON has no attributes, so every converter adopts a convention, and conventions differ between tools. The table lists the ones the site's two converters use.
In the JSON to XML direction the tool wraps everything in an element named root, never generates attributes, and replaces characters that are not legal in element names with underscores: a key such as first name becomes first_name, and a key written in a non-Latin script becomes underscores only. Output for a format with fixed element names, namespaces or attributes, such as SOAP or RSS, needs hand editing or a library that takes an explicit mapping.
| Input | Output | What to watch |
|---|---|---|
| JSON "tag": ["a", "b"] | <tag>a</tag> <tag>b</tag> | An array becomes repeated elements with no wrapper |
| XML <tag>a</tag> appearing once | "tag": "a" | One element yields a string, two yield an array |
| JSON "age": 30, "ok": true | <age>30</age> <ok>true</ok> | Converted back, both are strings: "30" and "true" |
| JSON "items": [] | Nothing | An empty array leaves no trace |
| XML <user id="7">Ann</user> | "user": {"@attributes": {"id": "7"}, "#text": "Ann"} | Attributes are kept under a reserved key |
| XML <p>Hello <b>big</b> world</p> | "p": {"b": "big"} | Text between child elements is discarded |
Checks worth making before converting a real file
- Quote YAML strings that look like numbers, booleans or dates: version numbers, postal codes, country codes, times.
- Carry identifiers longer than 15 digits as strings; JavaScript-based tools round integers above 2^53.
- After XML to JSON, write code that accepts both a single value and an array for any element that may repeat.
- If a signature or checksum covers the exact bytes, verify it before formatting or converting, because any re-serialisation changes the bytes.