JSON, YAML & XML: Developer Data Format Guide
JSON, YAML and XML compared on real data: 380 pages from our tools directory in all three formats (sizes nearly equal once gzipped; YAML parsed about 30 times slower than JSON), YAML's type-guessing traps such as NO becoming false, and safe parsing.
The Three Formats in Brief
Here is the same record in each format, one entry from our own tool catalog:
{
"slug": "json-formatter",
"name": "JSON Formatter",
"free": true
}
slug: json-formatter
name: JSON Formatter
free: true
<tool slug="json-formatter" free="true">
<name>JSON Formatter</name>
</tool>
- JSON (RFC 8259) has four primitive types (string, number, boolean, null) and two structures (object, array). Strings need double quotes; there are no comments and no trailing commas, which keeps it unambiguous.
- YAML uses indentation instead of brackets and adds comments, multi-line strings and anchors for reuse. Indentation must be spaces; tabs are not allowed.
- XML uses nested tags with attributes, and brings namespaces, schema validation (XSD) and query and transform languages (XPath, XSLT).
Size and Speed, Measured
We serialized the 380 pages of our tools directory (slug, name, description) in all three formats and parsed each one with Python 3.12.3's standard JSON and XML parsers and PyYAML 6.0.3 with the C-based libyaml loader (median of repeated runs):
| Format | Size | Gzipped | Parse time |
|---|---|---|---|
| JSON (indented) | 125.1 KB | 33.8 KB | 0.61 ms |
| JSON (minified) | 109.1 KB | 33.2 KB | 0.56 ms |
| YAML | 107.6 KB | 33.1 KB | 17.26 ms |
| XML (indented) | 123.7 KB | 33.8 KB | 1.66 ms |
Two things stand out. Size differences mostly disappear once the file is compressed: all four versions land within about 2% of each other gzipped, so for anything served over HTTP the format barely changes the bytes on the wire. Parse time does differ: XML took about 3 times as long as JSON and YAML about 31 times, even with the C loader. That matters for large files and hot paths, not for a config file read once at startup.
YAML's Type Guessing
YAML decides types from how a value looks, and the rules changed between versions. YAML 1.1 treats yes, no, on and off as booleans, numbers with a leading zero as octal, and colon-separated numbers as base 60. YAML 1.2 (2009) dropped those rules, but many parsers still apply them, PyYAML included. The same file read by PyYAML 6.0.3 and by ruamel.yaml 0.19.1, which follows YAML 1.2:
| YAML line | PyYAML (YAML 1.1 rules) | ruamel.yaml (YAML 1.2) |
|---|---|---|
country: NO | False | 'NO' |
version: 1.10 | 1.1 | 1.1 |
time: 1:20 | 80 | '1:20' |
zip: 012345 | 5349 | 12345 |
enabled: on | True | 'on' |
answer: yes | True | 'yes' |
port: 8080 | 8080 | 8080 |
Norway's country code becomes False, a time becomes 80 and a postal code becomes an octal number. Some traps survive even YAML 1.2: 1.10 is the float 1.1, so version numbers lose their trailing zero, and 012345 still loses its leading zero. The fix works everywhere: quote any value that must stay a string (country: "NO", version: "1.10").
Parsing Untrusted Data Safely
- YAML: in PyYAML, loading with
UnsafeLoadercan construct arbitrary Python objects from tags in the file. Useyaml.safe_load()(orCSafeLoader) for anything you did not write yourself. - XML: document type definitions allow entity expansion attacks and external entities (XXE) that read local files or make network requests. OWASP's advice is to disable DTD processing for untrusted input; in Python, the
defusedxmlpackage does that. - JSON: parsing cannot run code, but duplicate keys are a grey area. RFC 8259 says object names should be unique and that parsers differ when they are not; Python keeps the last value. Validate against a schema when the data matters.
Choosing and Converting
Use JSON for APIs and machine-to-machine data: every language parses it, quickly. Use YAML for configuration people edit by hand, where comments earn their keep, and quote the values that matter. Use XML where an ecosystem already requires it: RSS and Atom feeds, sitemaps, SVG, EPUB and office documents are all XML, and our own sitemap.xml and feed.xml are no exception.
Converting JSON to YAML is safe, because YAML 1.2 is designed as a superset of JSON. YAML to JSON drops comments and expands anchors. XML to JSON is lossy, since JSON has no equivalent for attributes, namespaces or text mixed with elements, so pick a mapping convention and stick to it. The YAML ↔ JSON Converter, JSON Formatter, YAML Formatter and XML Formatter run in your browser.
- [1]IETF RFC 8259, The JavaScript Object Notation (JSON) Data Interchange Format ↗
- [2]YAML Language Development Team, YAML Ain't Markup Language version 1.2.2 ↗
- [3]YAML 1.1 type repository: boolean (yes/no/on/off) ↗
- [4]W3C, Extensible Markup Language (XML) 1.0, Fifth Edition ↗
- [5]PyYAML documentation: loading YAML (load vs safe_load) ↗
- [6]ruamel.yaml on PyPI (YAML 1.2 parser used for comparison) ↗
- [7]OWASP, XML External Entity Prevention Cheat Sheet ↗