How to convert JSON to YAML
JSON and YAML describe the same kinds of data: objects, lists, text, numbers, true or false, and null. To convert json to yaml, the tool reads your JSON, checks it, and writes the same data with YAML's indentation instead of braces, brackets and quotes.
The built-in example is a Kubernetes ConfigMap in one line of JSON. Converted with the default options, it gives:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
labels:
app: web
data:
LOG_LEVEL: info
ENABLE_CACHE: yes
PORT_MAP: 80:80
TIMEOUT: 1:30
VERSION: "1.10"
NOTES: |-
line one
line twoThe status line reports 13 keys, 3 levels deep and 15 lines. Multi-line text becomes a block with a pipe, and VERSION keeps its quotes because an unquoted 1.10 would be read as the number 1.1.
Convert JSON to YAML online without breaking values
Look again at the output above. ENABLE_CACHE: yes and TIMEOUT: 1:30 are plain text in YAML 1.2, the current version, so they are correct. Many tools still read YAML 1.1, though. PyYAML, which Ansible uses, is one of them. These parsers turn yes, no, on and off into true and false, and read 1:30 as a time in seconds, so a value meant as text changes type.
Set Compatibility to Also safe for YAML 1.1 and those values are quoted:
ENABLE_CACHE: "yes"
PORT_MAP: 80:80
TIMEOUT: "1:30"
VERSION: "1.10"The result reads back as the same text in both versions. This is the option to use when you are not sure which parser will read the file. It also quotes dates such as 2020-01-01, and the words y, n, on, off, true, false and null, because some parsers turn them into other types.
JSON to YAML options explained
- Indent: 2 or 4 spaces for each nested level. Lists inside objects are indented under their key.
- Text values: quote only when YAML needs it, or always use double or single quotes. Keys are never forced into quotes.
- Compatibility: YAML 1.2 only, or also safe for YAML 1.1.
- Sort keys: puts the keys of every object in alphabetical order, which makes two files easier to compare.
- Wrap long lines: folds long text at 80 characters. Off by default, so each value stays on one line.
- Allow comments and trailing commas: reads JSON written by hand, such as a tsconfig or a VS Code settings file, by removing
//and/* */comments and a comma before a closing bracket first. - Split a top-level array into documents: writes each item as its own document, separated by
---. Useful for turning a list of Kubernetes objects into one multi-document file.
JSON errors with line and column
Most JSON problems come from habits learned in other languages. The tool names the exact spot and gives a hint:
- A comma before a closing bracket or brace, such as
{"a":1,}, is not allowed. - Single quotes around text or keys are not allowed. JSON needs double quotes.
- Keys must be in double quotes.
{a:1}is JavaScript, not JSON. - Comments are not part of JSON. Turn on Allow comments and trailing commas if your file has them.
undefined,NaNandInfinitydo not exist in JSON. Use null or a number.- Several JSON values in a row, as in a log file, need to be wrapped in a list.
JSON to YAML in Python and on the command line
For one file on your own machine, a command line tool works. With the yq tool installed:
yq -P config.json > config.yamlIn Python, with PyYAML installed:
import json, yaml
with open('config.json') as f:
data = json.load(f)
print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True))The page does the same job without installing anything, and shows the result as you edit. To go the other way, use YAML to JSON. For a config format that allows comments and has no indentation rules, JSON to TOML does the same job. If your JSON is stored as an escaped string inside another file, JSON Unescape turns it back into plain JSON first, and JSON Escape does the reverse. For files with one JSON object per line, JSONL Viewer helps you read them.
Limits
- JSON has no comments, so YAML comments cannot be created. Comments in your JSON are dropped when you turn on the lenient option.
- Whole numbers longer than 15 digits are rounded when JSON is read. The tool warns when it sees one.
- Keys that look like whole numbers, such as 10 or 2, are moved to the front in numeric order. That comes from how JavaScript stores object keys, so it affects any tool built on JavaScript objects.
- If the same key appears twice in one object, the last value wins.
- The tool does not create anchors or aliases. Repeated data is written out in full each time.
Your JSON is converted in your browser. It is not uploaded or stored. See the Privacy Policy for details.