The Same Data in Three Formats
Before comparing features, see how the same user profile looks in each format:
JSON
1{2 "user": {3 "name": "Alice Chen",4 "age": 28,5 "roles": ["admin", "editor"],6 "active": true7 }8}XML
1<?xml version="1.0" encoding="UTF-8"?>2<user>3 <name>Alice Chen</name>4 <age>28</age>5 <roles>6 <role>admin</role>7 <role>editor</role>8 </roles>9 <active>true</active>10</user>YAML
1# User profile2user:3 name: Alice Chen4 age: 285 roles:6 - admin7 - editor8 active: trueKey Observations
Feature-by-Feature Comparison
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Comments | ✗ Not supported | ✓ <!-- comment --> | ✓ # comment |
| Data types | 6 types (string, number, bool, null, object, array) | Everything is text | Auto-detected (can be surprising) |
| Schema validation | JSON Schema | XSD, DTD, RelaxNG | No standard schema |
| Namespaces | ✗ | ✓ (xmlns) | ✗ |
| Attributes | ✗ | ✓ (key="value") | ✗ |
| Mixed content | ✗ | ✓ (<p>text <b>bold</b> text</p>) | ✗ |
| Array syntax | ["a", "b"] | <items><item>a</item>...</items> | - a\n- b |
| Null value | null | No standard (empty tag) | null / ~ / empty |
| Multi-line strings | Escape with \n | CDATA sections | | or > block scalars |
| Parser speed | Very fast | Moderate | Slow |
| File size | Small | Large (verbose tags) | Smallest |
| Human readability | Good | Fair | Excellent |
| Whitespace significant | No | No | Yes (indentation) |
Decision Framework
When to Use JSON
REST APIs
Industry standard for request/response bodies
Browser ↔ Server
Native JSON.parse() in every browser
NoSQL databases
MongoDB, CouchDB, DynamoDB store JSON natively
Package manifests
package.json, composer.json, Cargo.json
TypeScript/Zod schemas
Tight integration with JS type systems
Inter-service communication
Lightweight, fast to serialize/deserialize
When to Use XML
Document markup
HTML, XHTML, SVG, MathML, DocBook
SOAP APIs
Enterprise integrations, banking, healthcare
RSS / Atom feeds
Content syndication is XML-based
Android layouts
UI definitions in Android apps
Office documents
OOXML (.docx, .xlsx) uses XML internally
Strict contract validation
XSD schema validation is very mature
When to Use YAML
CI/CD pipelines
GitHub Actions, GitLab CI, CircleCI configs
Kubernetes manifests
Deployments, services, ingress rules
Docker Compose
Container orchestration definitions
Ansible playbooks
Infrastructure automation scripts
Hugo / Jekyll configs
Static site generator settings
OpenAPI specs
API documentation (YAML or JSON accepted)
YAML Gotchas
on/off, yes/no to booleans and 3.10 to 3.1 (a float). Always quote strings that look like other types: "3.10", "no".Try These Tools
Performance Benchmarks
Parsing 1 MB of equivalent data (Node.js 22, Apple M3):
| Format | Parse Time | File Size | Memory |
|---|---|---|---|
| JSON | ~8 ms | 1.0 MB | ~4 MB |
| XML (fast-xml-parser) | ~25 ms | 1.6 MB | ~8 MB |
| YAML (js-yaml) | ~95 ms | 0.9 MB | ~6 MB |
Tip
Try It — Parse This JSON
Try It Yourself
Valid JSON — try converting this structure mentally to XML or YAML