YAML vs JSON: Differences, Examples, and When to Use Each
JSON is usually the safer default for APIs and machine-to-machine data. YAML is often more comfortable for configuration maintained by people. Both can represent nested objects, arrays, strings, numbers, booleans, and null values, but they make different tradeoffs.
YAML vs JSON at a Glance
| Feature | JSON | YAML |
|---|---|---|
| Best fit | APIs, browser data, generated files | Human-edited configuration |
| Structure | Braces, brackets, commas | Indentation and markers |
| Comments | Not supported | Supported with # |
| Parsing | Simple and widely built in | More complex; library required in many languages |
| Whitespace | Mostly insignificant | Indentation defines structure |
| Duplicate keys | Ambiguous and best avoided | Ambiguous and best avoided |
| Multiple documents | One value per document | Multiple documents supported |
| Anchors and aliases | No | Yes |
| File extensions | .json |
.yaml, .yml |
The Same Data in JSON and YAML
JSON:
{
"service": "catalog",
"port": 8080,
"features": ["search", "recommendations"],
"database": {
"host": "db.internal",
"ssl": true
}
}
YAML:
service: catalog
port: 8080
features:
- search
- recommendations
database:
host: db.internal
ssl: true
Convert between them with JSON to YAML and YAML to JSON.
When JSON Is the Better Choice
APIs and Web Applications
Browsers provide native JSON.parse() and JSON.stringify() methods, and most web frameworks treat application/json as a first-class content type. JSON’s explicit delimiters also make it easier to generate consistently.
Data Produced by Software
If people rarely edit a file, JSON’s extra punctuation is not a drawback. Its smaller feature set reduces differences between parsers and makes interoperability easier.
Strict Validation
JSON Schema provides a mature way to describe and validate JSON documents. Use the JSON Schema Validator to test an instance against a schema.
Untrusted Input
Every parser must still be configured safely, but JSON has fewer powerful language features than YAML. That smaller surface is useful when accepting data from outside your system.
When YAML Is the Better Choice
Human-Edited Configuration
YAML avoids most braces and quotation marks, supports comments, and can be easier to scan in deployment manifests and CI configuration.
Long Multiline Text
YAML has literal and folded block styles that are easier to maintain than newline escapes inside JSON strings.
Repeated Configuration
Anchors and aliases can reduce duplication, although they also make a document harder to understand when overused.
YAML Pitfalls
Indentation Is Syntax
This indentation changes the structure:
database:
host: db.internal
ssl: true
Here, ssl is a top-level property—not part of database.
Implicit Types Can Surprise You
YAML versions and libraries have historically differed in how they interpret values that look like dates, booleans, or numbers. Quote values when their exact string form matters, and validate the parsed result.
Unsafe Deserialization
Use a parser’s safe-loading mode for untrusted YAML. Some YAML libraries support custom tags that can construct language-specific objects; those features should not be enabled for arbitrary input.
JSON Pitfalls
- Comments and trailing commas are invalid.
- Keys and strings require double quotes.
- Dates and binary data require string conventions.
- A long nested document can become punctuation-heavy.
- Duplicate object names are not reliably interoperable.
Validate JSON with JSONLint, and convert commented JSONC before using a strict parser.
Is JSON a Subset of YAML?
JSON syntax is compatible with YAML 1.2, so a YAML 1.2 parser can generally read JSON. The reverse is not true: JSON parsers cannot read YAML indentation, comments, anchors, or unquoted keys.
Compatibility claims still depend on the parser and YAML version in use. Test the actual toolchain rather than assuming every YAML implementation behaves identically.
Choosing for Common Use Cases
| Use case | Recommended default |
|---|---|
| REST or browser API | JSON |
| Kubernetes manifest | YAML |
package.json or web metadata |
JSON |
| CI/CD workflow | YAML when required by the platform |
| Data export between unrelated systems | JSON |
| Configuration with extensive comments | YAML or JSONC |
| Streaming records | JSON Lines/NDJSON |
Migration Checklist
When converting formats:
- Check how dates, nulls, and number-like strings were parsed.
- Remove or relocate comments when converting to JSON.
- Expand YAML anchors before sending data to a JSON-only consumer.
- Validate the converted document against a schema or application test.
- Review secrets before pasting configuration into any online tool.
Frequently Asked Questions
Is YAML more readable than JSON?
Often, especially for configuration. Readability depends on consistent indentation and restrained use of advanced YAML features.
Is JSON faster than YAML?
JSON parsers are often faster because the grammar is smaller, but performance depends on the library, language, document, and workload. Benchmark your actual application when parsing cost matters.
Can YAML contain comments?
Yes. YAML uses # for comments. Standard JSON has no comment syntax.
Should an API return YAML?
It can, but JSON has broader client support and is the conventional default. Offer YAML only when users benefit from it and the content type is explicit.
Related Tools and Guides
Related Articles
JSON vs XML: Key Differences, Examples, and When to Use Each
Compare JSON vs XML syntax, schemas, namespaces, performance, mixed content, and API support to choose the right data format.
JSON vs JSONB in PostgreSQL: Differences, Performance, and Indexing
Compare PostgreSQL json and jsonb storage, indexing, query performance, duplicate keys, and the cases where each data type fits.
JSON Format: Syntax, Data Types, Rules, and Examples
Learn JSON format and syntax with valid examples of objects, arrays, strings, numbers, booleans, null, escaping, nesting, and common mistakes.
Unexpected End of JSON Input: Causes and Fixes
Fix Unexpected end of JSON input errors caused by empty responses, truncated data, missing brackets, incomplete strings, and incorrect fetch handling.