What does a JSON to YAML converter do?
JSON is what APIs return and what code produces. YAML is what people edit: Kubernetes manifests, Helm values, GitHub Actions and GitLab pipelines, OpenAPI files, Ansible playbooks. The two formats hold the same data model, so converting JSON to YAML is a mechanical job, but a tedious one to do by hand, because every brace, quote and comma has to turn into the right amount of indentation.
The usual reason to convert is readability. A response copied from an API or from kubectl get -o json is a wall of braces, while the same data in YAML fits on half the screen, can take comments once you save it, and diffs cleanly in version control. Another reason is bootstrapping: you export a resource as JSON, convert it, and edit the YAML into the file you actually keep.
The hard part is quoting. YAML decides whether no, 123 or 2024-05-01 is a string, a number or a date by how it is written, so a converter must add quotes exactly where the text would otherwise change meaning. This page does that, keeps integers larger than 2^53 exact, and runs entirely in your browser: the JSON you paste is never uploaded.
How to convert JSON to YAML
- Paste the JSON in the input box. The page opens with a sample project object already loaded; the direction, JSON to YAML, is fixed at the top.
- The YAML appears in the output box as you type. Nested objects become indented blocks and arrays become lines starting with
-. - Pick the Indent: 2 spaces (the usual choice for Kubernetes and CI files) or 4 spaces. The tab option is offered but YAML forbids tabs, so the output uses 2 spaces in that case. There is no minify option here.
- Tick Sort keys to put the keys of every object in alphabetical order, which makes two files easy to compare. Leave it off to keep the order of the JSON.
- Press Copy or Download to save
converted.yaml. - If the JSON is broken, a red box shows the line and column where reading stopped, and the line itself. The input must be strict JSON: unlike the JSON formatter, this page does not repair broken text for you.
JSON to YAML examples
An API object, default options
Input:
{"name":"api","replicas":3,"env":{"LOG_LEVEL":"info","DEBUG":false},"ports":[80,443],"labels":[]}Output:
name: api
replicas: 3
env:
LOG_LEVEL: info
DEBUG: false
ports:
- 80
- 443
labels: []An empty array stays [] on one line.
Strings that need quotes
The JSON {"a":"true","b":"123","e":"a: b","f":"# not comment","g":"","j":"- x","i":"line1\nline2"} becomes:
a: "true"
b: "123"
e: "a: b"
f: "# not comment"
g: ""
j: "- x"
i: |-
line1
line2Quotes appear where an unquoted value would turn into a boolean, a number, a mapping, a comment or a list item. A string with a line break is written as a literal block (|-).
An array of objects
{"users":[{"name":"Ana","roles":["admin","dev"]},{"name":"Bia","roles":[]}]} becomes:
users:
- name: Ana
roles:
- admin
- dev
- name: Bia
roles: []Indent 4 and Sort keys
{"z":1,"a":{"y":2,"b":3}} with Sort keys on gives a: first, then its keys b and y, then z: 1. With 4 spaces, the children of a sit four columns in.
Words that old parsers misread
The JSON string "no" is written as plain no, and "2024-05-01" as 2024-05-01, because YAML 1.2 reads both as text. A YAML 1.1 parser such as PyYAML reads no as false and the date as a date. If the file goes to such a tool, add the quotes yourself.
Common errors and how to fix them
- Quoted object key expected but got '}' (line 1, column 8): a trailing comma, as in
{"a":1,}. Remove the last comma. - Quoted object key expected but got ''': single quotes, or an unquoted key, as in
{'a':1}or{a:1}. JSON needs double quotes around keys and strings. - Comma ',' expected after value: two members without a comma between them (
{"a":1 "b":2}). Add it at the column shown. - Has more digits than YAML can keep exactly: a decimal such as
0.1234567890123456789would be rounded by YAML readers, so the converter stops and names the path, for example$.price. Put the number in quotes in the JSON if it is an identifier, or keep it in JSON. - Valid JSON that is not an object: a bare
"text"or42converts fine, but is a one-line YAML document. Check that you pasted the whole response and not a single field.
Frequently asked questions
How do I convert JSON to YAML online?
Paste the JSON in the input box, check the result and press Copy, or download converted.yaml. No account, and nothing leaves your browser. For the reverse, use YAML to JSON.
Is JSON valid YAML?
In practice yes, since YAML 1.2 was designed to include JSON, so many YAML tools accept a JSON file as it is. Converting still pays off: the result is easier to read and can hold comments.
Why are some strings quoted and others not?
The converter quotes only when it has to. Text that looks like true, null, a number, contains : or #, starts with - , or is empty would change meaning without quotes. Everything else is written plain.
Does the order of keys stay the same?
Yes, unless you tick Sort keys. JSON objects have no guaranteed order in the standard, but in practice parsers keep the order they read, and so does this page.
What about Kubernetes and OpenAPI files?
Both accept JSON and YAML. Convert the JSON, then validate the result with the tool that will consume it. To turn a document into TOML, use JSON to TOML; to check JSON first, use the JSON validator.
How to convert JSON to YAML in code
JavaScript
import { stringify } from 'yaml';
const json = '{"name":"api","replicas":3,"ports":[80,443]}';
console.log(stringify(JSON.parse(json), { indent: 2 }));Python
safe_dump sorts keys unless you pass sort_keys=False, and escapes non-ASCII unless you pass allow_unicode=True.
import json
import yaml
data = json.loads('{"nome":"João","itens":[1,2]}')
print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True))Go
var data any
if err := json.Unmarshal([]byte(`{"name":"api","ports":[80,443]}`), &data); err != nil {
log.Fatal(err)
}
out, _ := yaml.Marshal(data) // gopkg.in/yaml.v3; map keys come out sorted
fmt.Println(string(out))Bash
yq -P '.' response.json > response.yaml # mikefarah/yq v4: -P pretty-prints as YAML