YAML to JSON converter

Simply convert YAML to JSON with this online live converter.

loading YAML to JSON converter
About this tool

What does a YAML to JSON converter do?

YAML and JSON describe the same kind of data: objects, lists, strings, numbers, booleans and null. YAML is the format people like to write by hand, with indentation instead of braces, comments and multi-line text. JSON is the format programs like to read: every parser, API client, database column and validation library understands it. Converting YAML to JSON is how you hand a configuration file to something that only speaks JSON.

Typical jobs: turning a Kubernetes manifest, a docker-compose.yml or a GitHub Actions workflow into JSON so you can query it with jq; sending an OpenAPI description to a tool that wants JSON; loading an application config in a language without a good YAML library; or just checking what a tricky YAML file really means once the indentation and quoting rules have been applied. The JSON you get is the parsed result, so it is also a fast way to see whether a value became a string, a number or a boolean.

The converter runs in your browser: the YAML you paste is never uploaded. It reads YAML 1.2, expands anchors and aliases, keeps integers larger than 2^53 exact and shows the error with line and column when the YAML is wrong. Only your options (indent, sort keys, minify) are remembered on your device.

How to convert YAML to JSON

  1. Paste your YAML into the input box on the left. The page opens with a small sample already loaded, and the direction (YAML to JSON) is fixed at the top, so there is no need to choose a format.
  2. The JSON appears in the output box as you type. On a phone it sits below the input.
  3. Choose Indent: 2 spaces, 4 spaces or a tab. The choice is hidden while Minify is on.
  4. Tick Sort keys to order the keys of every object alphabetically (useful when you want to compare two files), or leave it off to keep the order of the YAML.
  5. Tick Minify to get the whole document on one line, ready to paste into a request body or an environment variable.
  6. Use Copy, or Download to save the result as converted.json.
  7. If the YAML is invalid, a red box names the line and column, shows the offending line and says what the parser expected. The output updates again as soon as you fix it.

YAML to JSON examples

A docker-compose service

Input:

services:
  web:
    image: nginx:1.27
    ports:
      - "8080:80"
    environment:
      DEBUG: true
      RETRIES: 3

Output:

{
  "services": {
    "web": {
      "image": "nginx:1.27",
      "ports": [
        "8080:80"
      ],
      "environment": {
        "DEBUG": true,
        "RETRIES": 3
      }
    }
  }
}

The quoted "8080:80" stays a string, while true and 3 become a boolean and a number.

Anchors and aliases are expanded

defaults: &defaults
  adapter: postgres
  pool: 5
development:
  <<: *defaults
  database: app_dev

The alias is replaced by a copy of the anchored mapping. The << merge key is not merged into its parent: it stays as a key named << that holds the copied values.

{
  "defaults": { "adapter": "postgres", "pool": 5 },
  "development": {
    "<<": { "adapter": "postgres", "pool": 5 },
    "database": "app_dev"
  }
}

If your consumer expects the merged shape, copy the keys up by hand after converting.

Values that are not what they look like

Input a: yes, b: on, c: true, d: 2024-05-01, e: 0755, f: "007", g: ~ gives:

YAMLJSONWhy
yes, on"yes", "on"YAML 1.2 only reads true and false as booleans
2024-05-01"2024-05-01"dates stay text, JSON has no date type
0755755a leading zero is not octal in YAML 1.2 (that is 0o755)
"007""007"quotes force a string
~null~ and null are both null

Common YAML errors and how to fix them

  • Tabs are not allowed as indentation (line 2, column 1): YAML indents with spaces only. Replace the tab with spaces; most editors have a "convert indentation to spaces" command.
  • All mapping items must start at the same column: one key is indented one space less or more than its siblings, as in c: 2 below b: 1. Align them.
  • Map keys must be unique: the same key appears twice at the same level (a: 1 then a: 2). JSON parsers disagree about duplicates, so the converter refuses them. Delete or rename one.
  • Nested mappings are not allowed in compact mappings: a value contains : as in msg: hello: world. Quote the value: msg: "hello: world".
  • Source contains multiple documents: the file has --- separators, as Kubernetes bundles do. Convert one document at a time, because JSON has no multi-document form.
  • A number with too many digits: a decimal with more digits than a double can hold is refused instead of being rounded silently. Quote it in the YAML to keep it as text.

Frequently asked questions

How do I convert a YAML file to JSON?

Paste the content in the input box or type it; the JSON appears next to it and you can copy it or download it as converted.json. No account is needed and nothing leaves the browser. To go the other way, use JSON to YAML.

Are YAML comments kept in the JSON?

No. JSON has no comments, so every # comment is dropped. The same happens with the original anchors: only the values they point to are kept.

Is YAML a superset of JSON?

Since YAML 1.2, a valid JSON document is also valid YAML in practice, which is why you can paste JSON into a YAML file. The reverse is not true: anchors, comments, multi-line blocks and multiple documents have no JSON form.

Why did my YAML boolean become a string?

YAML 1.2 treats only true and false as booleans. Words such as yes, no, on and off were booleans in YAML 1.1 and are plain strings here. Parsers that still follow 1.1, like PyYAML, will read the same file differently, so quote or rewrite those values.

Can I convert YAML to other formats?

Yes: YAML to TOML for configuration files, or the data converter for XML, CSV, .env and Properties. To only reformat a YAML file, use the YAML formatter.

How to convert YAML to JSON in code

JavaScript

The yaml package follows YAML 1.2, like this page.

import { parse } from 'yaml';

const data = parse('services:\n  web:\n    image: nginx:1.27\n');
console.log(JSON.stringify(data, null, 2));

Python

PyYAML is the common choice. Use safe_load, never load, for files you did not write.

import json
import yaml

with open("compose.yml") as f:
    data = yaml.safe_load(f)
print(json.dumps(data, indent=2, ensure_ascii=False, default=str))

default=str is needed because PyYAML turns 2024-05-01 into a date object, which json cannot write.

Bash

yq -o=json '.' compose.yml          # mikefarah/yq v4
python3 -c 'import sys,yaml,json; print(json.dumps(yaml.safe_load(sys.stdin)))' < compose.yml
Report a problem