Why Convert Between JSON and YAML?
JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) represent the same types of data structures but with very different syntax philosophies. Knowing when and how to convert between them is essential for modern development workflows.
JSON: The API Standard
JSON was designed for machine-to-machine communication. Its syntax is strict, unambiguous, and directly maps to JavaScript objects.
{
"name": "John Doe",
"age": 30,
"active": true,
"address": {
"city": "New York",
"country": "US"
},
"hobbies": ["reading", "coding"]
}
JSON strengths include universal support in every programming language, strict syntax that leaves no ambiguity, direct parseability in JavaScript, and compact size when minified.
JSON weaknesses include no support for comments (a significant limitation for configuration files) and more verbose syntax with quotes around all keys.
YAML: The Configuration Standard
YAML uses indentation and minimal punctuation to represent the same structures. It is the dominant format for configuration files in DevOps (Docker Compose, Kubernetes, GitHub Actions, Ansible).
name: John Doe
age: 30
active: true
address:
city: New York
country: US
hobbies:
- reading
- coding
YAML strengths include support for comments with the # character, more human-readable nested structures, natural multi-line strings, less punctuation, and support for references and anchors enabling DRY configuration.
YAML weaknesses include indentation-sensitivity (tabs are illegal), implicit type coercion that leads to bugs, multiple valid representations, and slower parsing.
Side-by-Side Comparison
| Feature | JSON | YAML |
|---|---|---|
| Comments | No | Yes (# comment) |
| Trailing commas | Not allowed | Not applicable |
| Multiline strings | Requires \n escape | Natural block style |
| Anchors / references | No | Yes (&anchor, *ref) |
| Primary use | APIs, data exchange | Config files |
| File extensions | .json | .yaml or .yml |
Common YAML Gotchas
Type Coercion
YAML automatically converts unquoted values to their natural types:
port: 8080 # Integer, not string!
enabled: yes # Boolean true, not string!
version: 1.0 # Float!
zip: 01234 # Integer (drops leading zero)!
To force strings, quote them: port: "8080", zip: "01234".
Norway Problem
In YAML 1.1, country codes NO, ON, OFF, YES, NO are parsed as booleans. YAML 1.2 fixed this.
Tabs Are Illegal
YAML strictly requires spaces for indentation. Tabs cause parse errors.
YAML Anchors and Aliases
YAML supports a DRY principle using anchors (&) and aliases (*):
defaults: &defaults
database: postgres
pool: 10
timeout: 30
development:
<<: *defaults
database: sqlite
production:
<<: *defaults
pool: 20
Multi-line Strings in YAML
# Literal block (|) - preserves newlines
description: |
First line
Second line
# Folded block (>) - converts newlines to spaces
description: >
This long sentence will
be joined as one line.
JSON to YAML Conversion Rules
| JSON | YAML |
|---|---|
| {} object | Key-value pairs with indentation |
| [] array | Items prefixed with - |
| "string" | Unquoted (or quoted if needed) |
| 123 number | 123 |
| true/false | true/false |
| null | null or ~ |
-> Try the JSON to YAML Converter