How to Convert Between JSON and YAML
JSON and YAML represent the same data model — objects, arrays, strings, numbers, booleans, and null. The choice between them is mostly about context: JSON is universal for APIs and data interchange; YAML is more readable for config files that humans write and edit. Converting between them is common.
When YAML is better than JSON
YAML is more readable for configuration files because:
- No required quotes around string keys and values
- No commas required between entries
- Supports comments (JSON doesn't)
- Multiline strings are easier to write
- Anchors and aliases allow reusing content without repetition
This is why Kubernetes, Docker Compose, GitHub Actions, Ansible, and most modern DevOps tooling use YAML for configuration. Writing a 200-line Kubernetes manifest in JSON would be significantly more verbose and harder to maintain.
When JSON is better than YAML
JSON is better for: API requests and responses, data interchange between systems, configuration that's machine-generated rather than human-edited, and contexts where YAML's flexibility is a liability rather than an asset.
YAML's flexibility is a known source of bugs. The Norway problem: country: NO in YAML parses NO as the boolean false, not the string "NO". YAML 1.2 fixed many of these implicit type coercion issues, but older parsers still exhibit them. JSON has no such ambiguities.
# YAML (readable, supports comments)
database:
host: localhost
port: 5432
name: myapp
# Override in production
pool_size: 10
# Equivalent JSON (more verbose, no comments)
{
"database": {
"host": "localhost",
"port": 5432,
"name": "myapp",
"pool_size": 10
}
}
Conversion gotchas
Types in YAML are inferred. A YAML value of true becomes a boolean. A value of "true" becomes a string. When converting YAML to JSON, implicit booleans (yes/no) in old YAML 1.1 become true/false in JSON, which may not be what you expected if they were supposed to be strings.
YAML anchors don't survive conversion to JSON. YAML anchors (&anchor) and aliases (*anchor) are YAML-specific features that allow reusing content. When converted to JSON, each anchor reference is expanded to its full value — which is functionally correct but loses the reuse structure.
Comments don't survive conversion to JSON. JSON has no comment syntax. YAML comments are stripped during conversion.
Frequently Asked Questions
How do I convert JSON to YAML online? Paste your JSON into an online JSON to YAML converter and it outputs the equivalent YAML. The conversion is structural — objects become mappings, arrays become sequences, and primitive values stay the same type.
How do I convert YAML to JSON? Use a YAML to JSON converter. Paste the YAML and the tool outputs valid JSON. This is useful when a YAML config needs to become a JSON request body, seed file, or input for a tool that does not accept YAML.
Is YAML a superset of JSON? Yes, YAML 1.2 is a strict superset of JSON, which means valid JSON is also valid YAML. Conversion from JSON to YAML is always safe. Going the other direction — YAML to JSON — can break if the YAML uses features JSON does not support, such as comments, anchors, or complex keys.
Why does my YAML have unexpected boolean values after converting from JSON? YAML automatically converts unquoted values like yes, no, true, false, on, and off into booleans. If your JSON had a string field with the value "yes", it may become the boolean true in YAML unless the value is quoted. Always check string fields that could be misread as booleans after conversion.
What is the difference between JSON and YAML? JSON uses braces, brackets, and quotes to define structure. YAML uses indentation and a cleaner syntax that is easier to read and write by hand. JSON is better for machine-to-machine data and APIs. YAML is better for config files, CI pipelines, and anything humans edit regularly.
Can I convert a Kubernetes YAML manifest to JSON? Yes. kubectl and most YAML to JSON converters handle Kubernetes manifests correctly. The Kubernetes API itself works in JSON internally. Converting manually is useful when you need to query or transform the manifest with JSON tools like jq.
Does JSON to YAML conversion lose any data? For standard JSON, no. All JSON value types — strings, numbers, booleans, nulls, arrays, and objects — have direct equivalents in YAML. You will not lose data unless the YAML tool post-processes values unexpectedly.
Which format should I use for config files — JSON or YAML? YAML is generally easier to maintain in config files because it supports comments, has less punctuation, and is easier to read. JSON is better when the config is machine-generated or consumed directly by APIs and tools that expect strict JSON.