How to use the JSON to YAML Converter

  1. Paste the JSON, or open a .json file.
  2. Choose the indent, the quote style and the line width.
  3. Turn on sorted keys, quoting every string or one document per array item if you need them.
  4. Fix any error at the line and column it names, then copy or download the YAML.

How JSON becomes YAML

Every JSON value has a YAML equivalent, so converting JSON to YAML means writing the same objects, arrays, strings, numbers, booleans and nulls in YAML's indented block style. The part that needs care is strings: an unquoted YAML value is read by its shape, so a JSON string such as "no", "0755" or "22:22" must be quoted in YAML or it stops being a string.

The JSON is read by a strict RFC 8259 parser that reports the line and column of any mistake, and the YAML is written by the yaml library as YAML 1.2.

Which strings get quotes, and why

A plain (unquoted) scalar is resolved by its content. The converter quotes every string that either a YAML 1.2 or a YAML 1.1 parser would read as something else, because files often end up in tools that use YAML 1.1: Ansible, PyYAML scripts, kubectl and docker-compose v1.

JSON stringUnquoted YAML would be read asWritten as
"yes", "no", "on", "off", "y"booleans in YAML 1.1"yes"
"true", "null", "~"boolean or null in both"true"
"0755", "1_000", "0b101"numbers in YAML 1.1"0755"
"22:22"a base-60 number (1342) in YAML 1.1"22:22"
"1.10", "1e3", ".inf"numbers"1.10"
"*", "- x", "a: b", "#tag"an alias, a list item, a mapping, a comment"*"
""null""

The same applies to keys: a JSON key "on", which GitHub Actions workflows use, is written "on": so YAML 1.1 tools do not see the boolean true. Other strings stay plain, which keeps the YAML readable.

The options

  • Indent: 2 spaces is the common style for Kubernetes and Compose; 4 is fine as long as a file is consistent.
  • Quotes: double quotes can escape anything ("\n", "\t"); single quotes are literal and only double the '. Strings with control characters always get double quotes.
  • Line width: long strings fold at 80 or 120 characters, or never.
  • Quote every string: some teams prefer it for configuration that people edit by hand.
  • Top-level array as documents: [{...}, {...}] becomes two YAML documents separated by ---, the form kubectl apply -f expects for several objects in one file.

Numbers are kept exactly

JSON allows integers of any size, but JavaScript's JSON.parse rounds anything above 2^53, so 12345678901234567890 silently becomes 12345678901234567000 in many online converters. This one keeps the digits. Key order is kept too, including keys like "10" and "2" that a JavaScript object would move to the front.

Common JSON errors

  • Trailing comma: {"a": 1,} is valid JavaScript but not JSON. Remove the comma.
  • Single quotes: JSON strings and keys use double quotes only.
  • Comments: // and /* */ are not JSON. Remove them, or paste the file into the YAML to JSON converter instead, since YAML accepts # comments.
  • Unquoted keys: {name: "x"} needs {"name": "x"}.
  • Line breaks inside a string: write \n.

A byte order mark at the start and blank lines at the end, which some Windows editors add, are ignored.

From the command line

yq -p json -o yaml '.' package.json > package.yaml    # mikefarah yq v4
python3 -c 'import sys, json, yaml; print(yaml.safe_dump(json.load(sys.stdin), sort_keys=False))' < data.json

With Shipown

Compose files are YAML, and Shipown deploys Compose projects to your server with the ports, environment values and volumes you review first. If a service definition starts life as JSON from an API or a generator, convert it here, then run it through the YAML validator before it is deployed.

Frequently asked questions

Why are some strings quoted in the YAML output?

Unquoted YAML values are read by their shape, so "no", "0755" or "22:22" would become a boolean or a number. Those strings are quoted so YAML 1.1 and YAML 1.2 parsers both read them back as strings.

Is all JSON valid YAML?

YAML 1.2 was designed as a superset of JSON, so nearly all JSON parses as YAML as it is. Converting it gives the indented block style people expect to read and edit.

Does the converter keep key order?

Yes, keys stay in the order they appear in the JSON, including numeric-looking keys. Turn on sorted keys if you want them alphabetical.

How do I get several YAML documents from one JSON array?

Turn on "Top-level array as separate documents". Each array item becomes its own document after a --- line, which is how Kubernetes files hold several objects.

What happens to very large numbers?

Integers larger than 2^53 are written digit for digit. Many converters built on JSON.parse round them.

Is my JSON sent to a server?

No. Parsing and conversion run in your browser.