Skip to content
Case Converter Online
en

JSON to YAML Converter

Paste JSON to get clean YAML, or switch direction to turn YAML into JSON. The conversion runs in your browser as you type.

Paste JSON or press Sample. Conversion runs in your browser.

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

  1. Choose a direction with the JSON to YAML or YAML to JSON button.
  2. Paste your data in the left box, press Open file, or press Sample. The result appears on the right as you type.
  3. Pick an Indent of 2 or 4 spaces. In the YAML to JSON direction there is also a Minified option.
  4. For JSON to YAML, turn on Sort keys A to Z to put keys in alphabetical order.
  5. Press Copy, or Download to save a .yaml or .json file.

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.

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.

JSON to YAML Converter: questions and answers

Why are some values in quotes in the YAML?

Strings that a YAML parser could read as something else get quotes. "02134" would become the number 2134, and "yes" would become true in older parsers. Quoting keeps them as text, so the YAML means exactly what the JSON meant.

What happens to comments when I convert YAML to JSON?

They are dropped, because JSON has no comments. If you convert back, the comments do not return. Keep your original YAML file if the comments matter.

Can I convert a file with several YAML documents?

Yes. Documents separated by --- become a JSON array, with one item per document, in the same order.

Are anchors and merge keys supported?

Yes. Aliases such as *defaults are replaced by the data they point to, and merge keys written as <<: *defaults are merged into the mapping, as Docker Compose and Rails expect.

Is my data uploaded?

No. Both directions run in your browser. Nothing you paste or open is sent anywhere.

More code and data tools

See all tools