How to convert JSON to YAML
Updated · 3 min read
YAML is the language of modern configuration: Kubernetes manifests, Docker Compose, GitHub Actions, GitLab CI, Ansible and OpenAPI specs. It is easier to read and comment than JSON, and every JSON document is also valid YAML data. This converter produces idiomatic YAML with two-space indentation, preserves key order and quotes only what needs quoting.
- Paste JSON or drop a
.jsonfile. - The YAML appears instantly; key order is preserved.
- Check values that were quoted — they would otherwise change type in YAML.
- Copy the YAML or download a
.yamlfile.
Example
JSON input
{
"services": {
"web": {
"image": "nginx:1.27",
"ports": ["8080:80"],
"environment": { "MODE": "production", "DEBUG": "false", "COUNTRY": "NO" }
}
}
}YAML output
services:
web:
image: nginx:1.27
ports:
- '8080:80'
environment:
MODE: production
DEBUG: 'false'
COUNTRY: 'NO'
Strings like "false" and "NO" are quoted in the YAML output. Left unquoted, many YAML parsers would read them as the boolean false — the infamous "Norway problem" where the country code NO turns into false.
Your data stays on your device. The conversion runs in JavaScript inside this tab — files and pasted text are never uploaded to DataSwitch Pro.
How values are written
- Strings are written plain whenever possible and quoted when they would otherwise be misread:
'true','02134','1.0', values starting with special characters, or containing:or#. - Numbers, booleans and null keep their JSON types.
- Objects become indented mappings; arrays become lists with
-markers. - Multi-line strings are preserved exactly.
Where YAML is used
Kubernetes and Helm, Docker Compose, GitHub Actions and GitLab CI pipelines, Ansible playbooks, OpenAPI/Swagger definitions, Home Assistant, static site generators such as Jekyll and Hugo front matter, and countless application config files. Converting from JSON is a quick way to start one of these files from an API response or generated data.
Adding comments after conversion
JSON has no comments, so the generated YAML has none either. One of the main reasons to switch to YAML is that you can now document your configuration with # comments.
Real-world examples
Write a Docker Compose file from JSON
Some tools export settings as JSON, while Docker Compose, Kubernetes and GitHub Actions expect YAML. Converting avoids retyping and indentation mistakes, and values such as 8080:80 are quoted where YAML needs it.
JSON
{
"services": {
"web": {
"image": "nginx:1.27",
"ports": ["8080:80"],
"restart": "unless-stopped"
}
}
}YAML
services:
web:
image: nginx:1.27
ports:
- '8080:80'
restart: unless-stopped
Make an API response readable
YAML drops braces and quotes, so a deeply nested API response becomes much easier to read and to paste into documentation or a support ticket.
JSON
{"user":{"id":7,"name":"Sam","roles":["editor","billing"],"active":true,"zip":"02134"}}YAML
user:
id: 7
name: Sam
roles:
- editor
- billing
active: true
zip: '02134'
Common errors and how to fix them
Strings like "yes", "no" or "02134" appear in quotes
They are quoted on purpose. Unquoted, older YAML parsers read yes as a boolean and 02134 as a number, which would change your data.
The YAML is rejected by the target tool
The conversion is valid YAML; the tool may expect a different structure, such as a list under ports. Compare with the tool's documented example.
Comments are missing
JSON cannot contain comments, so there are none to carry over. Add # comments to the YAML after converting.
Key order changed
It does not — keys are written in the same order as in the JSON. If the order looks different, check whether the source JSON was already sorted.
Related guides
Convert JSON to YAML in code
For one-off files the tool above is quickest. To automate the same task in a script or pipeline, these snippets are a good starting point.
Python (PyYAML)
import json
import yaml # pip install pyyaml
with open("data.json", encoding="utf-8") as f:
data = json.load(f)
print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True))
Command line (yq)
# mikefarah/yq v4
yq -p json -o yaml '.' data.json > data.yaml
Node.js (js-yaml)
import yaml from "js-yaml"; // npm install js-yaml
const out = yaml.dump(JSON.parse(text), { noRefs: true });
Frequently asked questions
Why are some strings in quotes?
Because without quotes YAML would interpret them as another type — for example yes, no, on, true, null or numbers with leading zeros. Quoting keeps them as strings, exactly as in your JSON.
Is key order preserved?
Yes. Keys appear in the same order as in the JSON input.
Can I convert a JSON array?
Yes. A top-level array becomes a YAML list. Each element starts with a dash.
Can I convert back to JSON?
Yes. Use the swap button or the YAML to JSON converter.
Is my configuration uploaded?
No. Conversion happens in your browser, so secrets in config files stay on your device.