JSON and YAML: same data, two styles
JSON and YAML can describe the same things: objects (called mappings in YAML), lists, strings, numbers, booleans and null. JSON uses braces, brackets, quotes and commas. YAML uses indentation and dashes, which makes it shorter and easier to read and edit by hand. That is why tools such as Kubernetes, Docker Compose, GitHub Actions and Ansible use YAML for configuration, while APIs usually speak JSON.
You often need to move between the two. An API returns JSON and you want it in a config file. Or a YAML config needs to be sent to a program that only reads JSON. This converter does both.
How to use it
- Choose a direction with the JSON to YAML or YAML to JSON button.
- Paste your data in the left box, press Open file, or press Sample. The result appears on the right as you type.
- Pick an Indent of 2 or 4 spaces. In the YAML to JSON direction there is also a Minified option.
- For JSON to YAML, turn on Sort keys A to Z to put keys in alphabetical order.
- Press Copy, or Download to save a
.yamlor.jsonfile.
When you switch direction, the current result moves into the input, so you can convert back and forth to check a round trip.
JSON to YAML example
{"name":"web-app","version":3,"private":true,"ports":[80,443],"database":{"host":"localhost","password":null},"tags":["api","v2"]}
becomes:
name: web-app
version: 3
private: true
ports:
- 80
- 443
database:
host: localhost
password: null
tags:
- api
- v2
Safe quoting
Some strings look like other types to a YAML parser. The converter quotes them so they stay strings. This JSON:
{"zip":"02134","enabled":"yes","version":"1.10","note":"a: b","empty":""}
becomes:
zip: "02134"
enabled: "yes"
version: "1.10"
note: "a: b"
empty: ""
Without the quotes, 02134 would lose its leading zero, 1.10 would become the number 1.1, and an older YAML 1.1 parser (PyYAML and many CI tools use those rules) would read yes as true. The converter follows the stricter quoting rules so the output reads the same in old and new parsers. Dates such as "2024-01-15" are quoted for the same reason.
YAML to JSON example
# App settings
name: web-app
ports:
- 80
- 443
debug: false
retries: 3
owner: ~
becomes:
{
"name": "web-app",
"ports": [
80,
443
],
"debug": false,
"retries": 3,
"owner": null
}
The comment is gone, because JSON has no comments, and ~ is YAML shorthand for null.
Merge keys are resolved. This Rails style file:
defaults: &defaults
adapter: postgres
host: localhost
development:
<<: *defaults
database: dev_db
gives:
{
"defaults": {
"adapter": "postgres",
"host": "localhost"
},
"development": {
"adapter": "postgres",
"host": "localhost",
"database": "dev_db"
}
}
Two documents separated by --- (for example kind: Service and kind: Deployment) become one array. With the Minified option it reads [{"kind":"Service"},{"kind":"Deployment"}].
How values are read
YAML to JSON uses the YAML 1.2 rules. Under those rules yes, no and on are plain strings, only true and false are booleans, and dates stay strings. This input:
a: yes
b: no
c: on
d: true
e: 010
f: 0o10
g: 1e3
h: 2024-01-15
converts (minified) to:
{"a":"yes","b":"no","c":"on","d":true,"e":10,"f":8,"g":1000,"h":"2024-01-15"}
Note that 010 is read as ten, and 0o10 is octal for eight.
If a value must stay text, put it in quotes in the YAML.
Tips for a clean round trip
- Converting JSON to YAML and back gives the same data, but not always the same text. Key order is kept unless you turn on Sort keys A to Z.
- YAML comments, anchors and custom tags have no JSON equivalent. Anchors are expanded into full copies of the data.
- Very large integers, such as 20-digit IDs, are kept exact in JSON to YAML where the browser supports it. In YAML to JSON they pass through JavaScript numbers, so store long IDs as quoted strings.
- Your files stay on your device. Nothing is uploaded.
Errors
Mistakes are reported with the line and column, and Show in input jumps to them. A trailing comma in JSON gives Line 3, column 1: Trailing comma before "}" is not allowed. In YAML, a repeated key gives Map keys must be unique, and tabs used for indentation are rejected because YAML only allows spaces.
Other ways to convert
yq. The Go version of yq converts on the command line: it reads JSON as input because JSON is valid YAML, and it can print either format.
Python. With the PyYAML package, yaml.safe_dump(json.load(f)) writes YAML from a JSON file. PyYAML follows YAML 1.1, so check strings like yes and no.
VS Code. Several extensions add commands to convert a selection between JSON and YAML.
Related tools
To tidy a YAML file without converting it, use the YAML Formatter. To check or pretty print JSON first, use the JSON Formatter. For spreadsheet data, start with CSV to JSON.