JSON vs XML vs YAML: Choosing the Right Data Format
Data formats are the backbone of modern software. Learn the differences between JSON, XML, and YAML to choose the right format for your APIs, configuration files, and data exchange needs.
Quick Comparison
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Readability | Good | Moderate | Excellent |
| Verbosity | Low | High | Very Low |
| Data Types | Basic (6 types) | All text | Rich (auto-detect) |
| Comments | No | Yes | Yes |
| Schema Support | JSON Schema | XSD, DTD | JSON Schema |
| Browser Native | Yes | Yes | No |
| Primary Use | APIs, Web | Documents, Enterprise | Configuration |
Same Data, Three Formats
JSON
{
"name": "John",
"age": 30,
"active": true,
"skills": ["JS", "Python"]
}XML
<person>
<name>John</name>
<age>30</age>
<active>true</active>
<skills>
<skill>JS</skill>
<skill>Python</skill>
</skills>
</person>YAML
name: John
age: 30
active: true
skills:
-
JS
-
Python
JSON Deep Dive
JavaScript Object Notation is the most popular data format for web APIs and modern applications.
Syntax Rules
{
"string": "Hello, World!",
"number": 42,
"float": 3.14,
"boolean": true,
"null": null,
"array": [1, 2, 3],
"object": {
"nested": "value"
}
}
Data Types
- String:
"text"(double quotes required) - Number:
42or3.14(no quotes) - Boolean:
trueorfalse - Null:
null - Array:
[1, 2, 3] - Object:
{"key": "value"}
Advantages
- Native JavaScript support (no parsing needed in browsers)
- Compact syntax with minimal overhead
- Excellent tooling and library support
- Type preservation (numbers stay numbers)
Limitations
- No comments allowed (use JSONC or JSON5 for comments)
- No date type (dates are strings)
- Trailing commas cause errors
- Keys must be quoted strings
Extensible Markup Language is a mature format widely used in enterprise systems and document markup.XML Deep Dive
Syntax Structure
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!-- This is a comment -->
<book id="1" category="fiction">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<price currency="USD">12.99</price>
</book>
</catalog>Key Concepts
- Elements:
<tag>content</tag> - Attributes:
<tag attr="value"> - Self-closing:
<tag /> - Comments:
<!-- comment --> - CDATA:
<![CDATA[raw text]]>
Advantages
- Powerful schema validation (XSD, DTD)
- Supports mixed content (text + elements)
- Namespaces for avoiding conflicts
- Excellent for document-centric data
- XSLT for transformations
Limitations
- Verbose - lots of closing tags
- All data is text (no native types)
- Complex to parse correctly
- Larger file sizes
When XML Excels
- SOAP web services
- Office documents (DOCX, XLSX are XML inside)
- Configuration (Android, Spring, Maven)
- Publishing (XHTML, SVG, RSS)
XML Tools
YAML Ain't Markup Language prioritizes human readability, making it ideal for configuration files.YAML Deep Dive
Syntax Basics
# This is a comment
name: John Doe
age: 30
active: true
# Lists
skills:
- JavaScript
- Python
- Go
# Nested objects
address:
city: New York
zip: 10001
# Inline syntax
colors: [red, green, blue]
person: {name: Jane, age: 25}
# Multi-line strings
description: |
This is a multi-line
string that preserves
line breaks.
summary: >
This is a folded string
that becomes a single
line with spaces.
Data Types (Auto-detected)
# Strings (quotes optional for simple text)
name: John
quoted: "with: special chars"
# Numbers
integer: 42
float: 3.14
scientific: 1.2e+5
# Booleans
enabled: true
disabled: false
also_true: yes # YAML-specific
also_false: no
# Null
nothing: null
also_null: ~
# Dates (ISO 8601)
date: 2024-03-15
datetime: 2024-03-15T10:30:00Z
Advantages
- Extremely readable and writable by humans
- Comments are supported
- No brackets, quotes, or commas needed
- Rich data types including dates
- Anchors and aliases for reuse
Limitations
- Indentation is significant (spaces only, no tabs)
- Not supported natively in browsers
- Security concerns (arbitrary code execution in some parsers)
- Subtle syntax errors can be hard to spot
Common Uses
- Kubernetes: All manifests are YAML
- Docker Compose: Service definitions
- GitHub Actions: Workflow files
- CI/CD: GitLab CI, Azure Pipelines
- Ansible: Playbooks and inventories
When to Use Each Format
- REST APIs
- Web applications
- JavaScript projects
- Mobile app data
- NoSQL databases
- Package files (package.json)
- SOAP services
- Enterprise integration
- Document formats
- Complex schemas
- Legacy systems
- Publishing (RSS, SVG)
- Configuration files
- Kubernetes/Docker
- CI/CD pipelines
- Ansible playbooks
- Human-edited files
- Documentation
Decision Flowchart
Is this for a web API?
├── YES → JSON
└── NO → Is this a configuration file?
├── YES → Will humans edit it frequently?
│ ├── YES → YAML
│ └── NO → JSON or YAML
└── NO → Is this document-centric data?
├── YES → XML
└── NO → Is this for JavaScript?
├── YES → JSON
└── NO → JSON or YAML (your preference)Most conversions are straightforward, but watch for these edge cases:Converting Between Formats
JSON ↔ YAML
- Nearly perfect conversion both ways
- YAML comments are lost when converting to JSON
- YAML's special types (dates) become strings in JSON
JSON/YAML → XML
- Arrays become repeated elements
- Object keys become element names
- Need to decide: attributes vs. elements
XML → JSON/YAML
- Attributes often become special keys (e.g., @attr)
- Mixed content is tricky to represent
- Namespaces may be lost or simplified
Best Practices
JSON Best Practices
- Use consistent naming (camelCase or snake_case)
- Keep nesting depth manageable (≤4 levels)
- Use arrays for ordered collections, objects for key-value
- Include API version in responses
- Use ISO 8601 for dates:
"2024-03-15T10:30:00Z"
YAML Best Practices
- Use 2 spaces for indentation (never tabs)
- Quote strings that might be misinterpreted (
"yes","1.0") - Use comments liberally for documentation
- Keep files focused (split large configs)
- Validate before deploying
XML Best Practices
- Always include XML declaration
- Use meaningful element names
- Prefer elements over attributes for data
- Use namespaces to avoid conflicts
- Validate against schema (XSD)
Data Format Tools