Three formats, one underlying shape
JSON, YAML, and XML all describe the same basic set of structures — objects/maps, arrays/lists, and scalar values (strings, numbers, booleans). What differs is syntax, type strictness, and what extra features each format bolts on. Converting between them usually works, but "usually" is the operative word — each has quirks that don't map cleanly onto the others.
JSON: strict, minimal, ubiquitous
{
"name": "ToolSphere",
"version": 2,
"active": true,
"tags": ["json", "developer-tools"]
}
JSON has exactly six data types (object, array, string, number, boolean, null), no comments, and strict syntax — trailing commas and unquoted keys are both invalid. This rigidity is a feature: it's what makes JSON trivially parseable and the default for virtually every web API.
YAML: readable, whitespace-significant, feature-rich
name: ToolSphere
version: 2
active: true
tags:
- json
- developer-tools
YAML is a superset of JSON's data model (valid JSON is technically valid YAML) but adds significant extra machinery: comments (#), anchors/aliases for reusing blocks, multi-document files (--- separators), and multiple ways to write the same string. Indentation is semantic — two lines at different indent levels are structurally different, not just cosmetically. This is exactly why YAML files break from a single misplaced space in a way JSON files can't.
XML: verbose, but self-describing and schema-able
<config>
<name>ToolSphere</name>
<version>2</version>
<active>true</active>
<tags>
<tag>json</tag>
<tag>developer-tools</tag>
</tags>
</config>
XML has no native concept of "array" — repeated elements just imply a list — and everything is technically a string until a schema (XSD) says otherwise. What XML has that JSON and YAML don't: attributes (<tag id="1">), namespaces, and mature validation/transformation tooling (XSD, XSLT), which is why it persists in enterprise, SOAP, and document-centric systems (like RSS/Atom feeds) despite being more verbose.
Where conversions get lossy
- JSON → XML: JSON has no attributes, so a converter must invent a convention (e.g., keys prefixed with
@) — different tools do this differently, meaning round-tripping through two different converters can change structure. - YAML → JSON: comments are silently dropped (JSON has none), and YAML's multiple ways to write the same scalar (
yes/true/oncan all mean booleantruedepending on the YAML version) can resolve unexpectedly. - Any → XML with repeated keys: since XML has no native arrays, a JSON array of objects and a JSON object with array-like repeated keys can both become structurally ambiguous once converted.
- Numbers vs. strings: YAML and XML are more permissive about unquoted scalars; a value like
01234might convert to the number1234(dropping the leading zero) or stay a string depending on the converter's type inference — a classic zip-code or ID corruption bug.
Common mistakes
- Trusting round-trip conversions blindly. Convert JSON → YAML → JSON and diff the result before assuming nothing changed, especially with unquoted string edge cases (
no,off, version-like strings). - Ignoring YAML's indentation sensitivity. A tab character mixed with spaces, or an indent that's off by one space, produces a different structure — not an error — which can silently corrupt config files.
- Forgetting XML attributes vs. elements is a design choice, not a technical requirement, meaning two "equally valid" XML representations of the same data can require different parsing logic downstream.
FAQ
Is YAML strictly more powerful than JSON?
For data modeling, YAML's superset relationship means yes — but that power (comments, anchors, multiple scalar styles) is exactly what introduces conversion ambiguity that pure JSON doesn't have.
Why does my YAML config break with a single extra space?
YAML uses indentation to express structure, the same way Python uses it for code blocks — an inconsistent indent level is a structural change, not whitespace noise.
Can every JSON document convert cleanly to XML?
Mostly, but arrays and lack of attributes force conversion tools to make assumptions; always verify structure after converting, especially for deeply nested or attribute-heavy source data.
Which format should I pick for a new config file?
YAML for human-edited config (comments matter), JSON for anything machine-generated or API-facing (unambiguous, no whitespace sensitivity), XML mainly when you need namespaces/schema validation or you're integrating with a system that already speaks XML.
Convert and validate between formats with the YAML Formatter and JSON Toolkit — both run entirely in your browser.