JSON to YAML Converter Guide: Syntax, Differences & Migration Tips
Quick Answer
- *YAML is a superset of JSON since YAML 1.2 — all valid JSON is valid YAML.
- *YAML uses indentation instead of braces, supports comments with
#, and is more human-readable. - *94% of Kubernetes users write configs in YAML over JSON (2025 CNCF Survey).
- *Watch out for the Norway problem — unquoted “no” in YAML becomes boolean
false.
JSON vs YAML: Side-by-Side Comparison
Both JSON and YAML are data serialization formats. JSON was formalized by Douglas Crockford in 2001 and standardized as RFC 8259. YAML (originally “Yet Another Markup Language,” later backronymed to “YAML Ain't Markup Language”) first appeared in 2001 and reached version 1.2 in 2009.
| Feature | JSON | YAML |
|---|---|---|
| Comments | Not supported | Supported (#) |
| Readability | Good for small docs | Better for large/nested docs |
| Data types | string, number, boolean, null, array, object | All JSON types + dates, timestamps, binary |
| Multi-line strings | Escaped newlines only | Block scalars (| and >) |
| Anchors/aliases | Not supported | Supported (& and *) |
| Trailing commas | Not allowed | No commas needed |
| File extension | .json | .yaml or .yml |
| Parse speed | Faster (simpler grammar) | Slower (complex grammar) |
According to the 2025 Stack Overflow Developer Survey, 72% of developerswork with JSON regularly, while 48% work with YAML. The overlap is significant — most developers use both, choosing the format based on context.
When to Use JSON
JSON dominates machine-to-machine communication. Use it for:
- REST APIs: JSON is the de facto standard. Over 90% of public APIs use JSON for request and response bodies (RapidAPI 2024 State of APIs report).
- Data interchange: Every programming language has a fast, built-in JSON parser. YAML parsers are external dependencies in most languages.
- Browser/frontend: JavaScript natively parses JSON with
JSON.parse(). No library needed. - Strict schemas: JSON Schema is widely supported for validation. YAML has no equivalent standard.
When to Use YAML
YAML excels where humans read and edit config files. Use it for:
- Kubernetes manifests: 94% of K8s users prefer YAML (CNCF 2025 Survey).
- CI/CD pipelines: GitHub Actions, GitLab CI, CircleCI, and most CI tools use YAML configs.
- Docker Compose: The standard
docker-compose.ymlformat. - Ansible playbooks: Ansible is YAML-native for infrastructure automation.
- Application configs: Rails, Spring Boot, Hugo, and many frameworks prefer YAML for settings.
Common Conversion Pitfalls
The Norway Problem
YAML 1.1 treats unquoted no, yes, on, off, true, and false as booleans. This means the ISO 3166-1 country code for Norway (NO) becomes false when parsed. YAML 1.2 fixed this, but many parsers still default to 1.1 behavior. Always quote strings that could be interpreted as booleans.
Indentation Errors
YAML requires spaces for indentation — tabs are not allowed. A single misaligned space can change the structure entirely. The YAML spec recommends 2-space indentation, which is also the most common convention in Kubernetes and Docker Compose files.
Unquoted Numbers and Versions
Version strings like 1.10 become the float 1.1 in YAML if unquoted. Octal numbers (e.g., 0123) are interpreted differently. Phone numbers lose leading zeros. Always quote values that look like numbers but are actually strings.
Multi-line String Conversion
JSON uses \n for newlines within strings. YAML offers two block scalar styles: literal block (|) preserves newlines exactly, and folded block (>) joins lines with spaces. Choosing the wrong style changes line break behavior.
Conversion Rules Reference
| JSON | YAML Equivalent | Notes |
|---|---|---|
{"key": "value"} | key: value | Quotes optional for simple strings |
{"n": 42} | n: 42 | Numbers are unquoted |
{"b": true} | b: true | Same boolean literals |
{"x": null} | x: null or x: ~ | Tilde is YAML shorthand for null |
[1, 2, 3] | - 1\n- 2\n- 3 | Block or flow style |
| Nested objects | Indented keys | 2-space indent is conventional |
YAML Features with No JSON Equivalent
Comments
YAML supports inline comments with #. This is the single biggest reason teams choose YAML for configuration. Documenting why a setting exists is critical for maintainability. Standard JSON has no comment syntax — Douglas Crockford intentionally excluded them.
Anchors and Aliases
YAML anchors (&) and aliases (*) let you define a value once and reference it elsewhere, reducing duplication. This is heavily used in CI/CD configs where multiple jobs share settings. JSON requires duplicating the data.
Multiple Documents
A single YAML file can contain multiple documents separated by ---. Kubernetes uses this pattern to define multiple resources in one file. JSON requires separate files or wrapping in an array.
Convert between JSON and YAML instantly
Use our free JSON to YAML Converter →Frequently Asked Questions
What is the difference between JSON and YAML?
JSON uses braces, brackets, and quotes for structure. YAML uses indentation and is designed to be more human-readable. JSON is a strict subset of YAML (since YAML 1.2), meaning all valid JSON is also valid YAML. JSON is better for APIs and machine-to-machine data exchange, while YAML is preferred for configuration files that humans edit frequently.
Is YAML a superset of JSON?
Yes, since YAML 1.2 (released in 2009). Every valid JSON document is also a valid YAML document. This means you can paste JSON directly into a YAML file and it will parse correctly. The reverse is not true — YAML features like anchors, aliases, comments, and multi-line strings have no JSON equivalent.
Why does Kubernetes use YAML instead of JSON?
Kubernetes accepts both JSON and YAML, but the community overwhelmingly uses YAML because it supports comments (critical for documenting infrastructure configs), is more readable for deeply nested structures, and requires fewer characters. The 2025 CNCF Survey found that 94% of Kubernetes users write manifests in YAML. Kubernetes internally converts all configs to JSON before processing.
What are common YAML pitfalls when converting from JSON?
The most common pitfalls are: (1) indentation errors — YAML uses spaces only, never tabs, and indentation level matters; (2) the Norway problem — unquoted “no,” “yes,” “on,” “off” are parsed as booleans, so country code “NO” becomes false; (3) unquoted strings that look like numbers — “1.0” becomes a float, version “1.10” becomes 1.1; (4) multi-line strings require special syntax (| for literal, > for folded).
Can I add comments in JSON?
Standard JSON (RFC 8259) does not support comments. This is a deliberate design decision by Douglas Crockford to prevent misuse of comments for parsing directives. YAML supports comments with the # character. Some JSON variants like JSON5 and JSONC (used by VS Code) do support comments, but they are not compatible with standard JSON parsers.