When creating config files or deciding on an API response format, have you ever wondered "which one should I use?" JSON, YAML, XML—all three express data, but when is each one the best choice?
Side-by-Side Comparison
Let's represent the same data in all three formats.
JSON
{
"name": "John Doe",
"age": 30,
"skills": ["JavaScript", "Python"],
"address": {
"city": "New York",
"zip": "10001"
}
}
YAML
name: John Doe age: 30 skills: - JavaScript - Python address: city: New York zip: "10001"
XML
<person>
<name>John Doe</name>
<age>30</age>
<skills>
<skill>JavaScript</skill>
<skill>Python</skill>
</skills>
<address>
<city>New York</city>
<zip>10001</zip>
</address>
</person>
You can already see the difference. YAML is the most concise, XML the most verbose.
JSON: The Web Standard
Pros:
- Native to JavaScript (it's literally JavaScript Object Notation)
- Supported by virtually every programming language
- Fast parsing
- De facto standard for web APIs
Cons:
- No comments (inconvenient for config files)
- Strict syntax (quotes, commas required)
- Tedious for humans to edit manually
When to use: API responses, web data exchange, programmatic data storage
YAML: Human-Friendly
Pros:
- Best readability (structure via indentation)
- Supports comments (with #)
- Quotes usually optional
- Multiline string support
Cons:
- Indentation errors break things (watch for tab/space mixing)
- Parsing slower than JSON
- Advanced features (anchors, aliases) can be confusing
When to use: Config files (Docker Compose, Kubernetes, GitHub Actions), files humans edit frequently
XML: Still Alive
Pros:
- Schema validation (strict type checking with XSD)
- Supports attributes
- Namespaces prevent collisions
- XSLT for transformations
Cons:
- Verbose (sometimes more tags than data...)
- Complex and slow parsing
- Rarely used in modern web
When to use: Legacy systems, SOAP APIs, when strict schemas are required, office document formats (docx is internally XML)
Summary Table
| JSON | YAML | XML | |
|---|---|---|---|
| Readability | Medium | Good | Poor |
| Comments | No | Yes | Yes |
| Parse Speed | Fast | Medium | Slow |
| Schema Validation | JSON Schema | Limited | XSD (powerful) |
| Primary Use | APIs, Web | Config files | Legacy, Docs |
Frequently Asked Questions
Which is better for config files, JSON or YAML?
YAML supports comments and uses indentation, making it human-readable. Many tools like Docker Compose, Kubernetes, and GitHub Actions use YAML. However, be careful with indentation errors.
Is XML still used today?
Yes, XML is widely used in SOAP web services, Android layouts, Maven/Gradle build files, SVG graphics, and Office documents (OOXML). It excels in complex document structures requiring namespaces and schema validation.
Can I use comments in JSON?
Standard JSON does not support comments. Extended formats like JSONC (JSON with Comments) and JSON5 allow comments. TypeScript's tsconfig.json and VS Code settings support JSONC.
What format is best for API responses?
JSON is the de facto standard. It parses quickly, has native support in most languages, and works well with web clients (JavaScript). Use JSON for REST APIs unless you have a specific reason not to.
How do I prevent YAML indentation errors?
Use 2 spaces instead of tabs. Install EditorConfig or YAML extensions in your IDE for auto-validation. Adding a YAML linter (yamllint) to your CI pipeline also helps.