How JSON to YAML conversion works
Conversion strips the four structural tokens JSON uses — curly braces, square brackets, commas, and most quotes — and rebuilds the same data tree using indentation. Object keys become indented mappings, so nesting depth is expressed purely by how far a line is pushed right (2 spaces per level here). Nothing about the data changes; only the syntax does.
{
"server": {
"host": "localhost",
"port": 8080,
"debug": true
}
}server:
host: localhost
port: 8080
debug: trueArrays become dash-prefixed sequences
A JSON array turns into a YAML sequence: each element gets a - prefix at the array's indentation level. For an array of objects, the object's first key sits on the same line as the dash and its remaining keys align underneath — a layout that is much easier to scan than bracket-and-comma JSON.
{
"plugins": [
{ "name": "auth", "enabled": true },
{ "name": "billing", "enabled": false }
]
}plugins:
- name: auth
enabled: true
- name: billing
enabled: falseWhy some strings stay quoted (type preservation)
In YAML a bare scalar like 1.0, true, or 2026-01-01 is parsed as a number, boolean, or date — not a string. To keep a JSON string a string, the converter quotes any value that would otherwise be coerced. The classic trap is a ZIP code: unquoted 00123 becomes the integer 123 and loses its leading zero. JSON null is written as the YAML tilde ~.
{
"enabled": true,
"retries": 3,
"version": "1.0",
"zip": "00123",
"deletedAt": null
}enabled: true
retries: 3
version: "1.0"
zip: "00123"
deletedAt: ~Long strings: folded and literal block scalars
JSON crams multi-line text into a single string with \\n escapes, which is hard to read. YAML offers block scalars instead: | (literal) keeps every newline as written, while > (folded) joins wrapped lines into spaces. This is the one case where YAML is genuinely more legible than the JSON it came from.
{
"script": "echo build\necho test\necho deploy",
"summary": "A long one-line description that you would rather wrap across several lines for readability."
}script: |
echo build
echo test
echo deploy
summary: >
A long one-line description that you would
rather wrap across several lines for readability.What gets lost: comments and anchors
The JSON to YAML to JSON round trip is lossless for data but lossy for authored formatting. JSON has no comment syntax, so a converter cannot invent the # comments and &anchor/*alias references a hand-written YAML config might use — and converting such YAML back to JSON silently drops them. Plan to re-add comments and anchors by hand after conversion.
# This comment and the anchor below CANNOT come from JSON
defaults: &defaults
retries: 3 # inline comment also lost on round trip
prod:
<<: *defaults
host: prod.example.comConvert JSON to YAML in code
When you need conversion in a script or CI pipeline instead of the browser tool:
# Python — PyYAML (pip install pyyaml)
import json, yaml
data = json.loads(json_string)
print(yaml.dump(data, default_flow_style=False, allow_unicode=True))
# default_flow_style=False forces block style; for YAML 1.2 use ruamel.yaml// Node.js — js-yaml (npm i js-yaml), YAML 1.2 by default in v4+
import yaml from 'js-yaml'
console.log(yaml.dump(JSON.parse(jsonString)))# Command line — yq, no script needed
yq -P -oy '.' data.json # -P prettifies, -oy outputs YAMLFor a deeper walkthrough see the JSON to YAML tutorial. To convert JSON to a different config format, try JSON to TOML.