Convert YAML to JSON!

Input YAML (yml) to convert it into JSON! Handy for checking Kubernetes and docker-compose configs.
YAML
Result
 

Convert YAML to JSON!

Convert a YAML (yml) document into JSON to see the structure that indentation hides and the actual type each value was parsed as. Anchors and aliases are expanded into real values, and multiple documents separated by --- are collected into an array.

It is especially handy for catching classic YAML mistakes: a value attached to the wrong parent because of one missing space, or version: 1.10 silently becoming the number 1.1.

How to use

  1. Paste a docker-compose.yml, a Kubernetes manifest or a workflow file into the YAML box, or drop the file onto it.
  2. Syntax errors appear in the result with line and column numbers; check the indentation at that spot first.
  3. Read the JSON under Result to see whether each value became a string, number or boolean, and copy it if you need it.

Example

Input (YAML)
name: ci
on:
  push:
    branches: [main]
defaults: &node
  runs-on: ubuntu-latest
  timeout-minutes: 10
jobs:
  test: *node
Output (JSON)
{
  "name": "ci",
  "on": {
    "push": {
      "branches": [
        "main"
      ]
    }
  },
  "defaults": {
    "runs-on": "ubuntu-latest",
    "timeout-minutes": 10
  },
  "jobs": {
    "test": {
      "runs-on": "ubuntu-latest",
      "timeout-minutes": 10
    }
  }
}

The GitHub Actions on: key stays the string key "on", because this tool parses with YAML 1.2 rules. The block defined with the &node anchor is expanded in place of *node.

Supported features

  • Parsed with the YAML 1.2 core schema: yes, on and off stay strings, and only true/True/false become booleans.
  • ~ and empty values become null; hex 0x1F and octal 0o17 become numbers.
  • &anchors and *aliases are resolved to the referenced values.
  • Literal | and folded > block scalars are turned into strings with YAML's newline rules applied.
  • Several ----separated documents become a JSON array; a single document is output as its own value.
  • A key that appears twice in the same mapping is reported as an error.

Use cases

Checking Kubernetes manifest indentation

If env under containers is indented one level too shallow, it silently attaches to another field and you only find out at apply time. In JSON, the braces show exactly which object it belongs to.

Using config from code

For a script or front-end bundle where adding a YAML parser is overkill, convert the config once and use it as import config from './config.json'.

Inspecting an OpenAPI spec

An OpenAPI document written in YAML is easier to query with JSONPath or jq once converted, for example to pull out the schema at a specific path.

Notes and limitations

  • Merge keys (<<: *defaults) are applied: the anchored mapping's keys are copied in, and keys written next to << override them. A YAML 1.2-only parser would keep << as a plain key, so results can differ from such tools.
  • Dates and times such as 2024-01-01 stay strings rather than becoming Date values.
  • .inf and .nan have no JSON representation and become null.
  • Comments cannot be carried into JSON and are dropped.
  • PyYAML (YAML 1.1) reads on and yes as true, so results from Python may differ.

FAQ

Why is GitHub Actions' on shown as "on" instead of true?

In YAML 1.2 the only booleans are true and false, so on is an ordinary string, which is also how GitHub Actions reads the key. Python's PyYAML follows 1.1 and turns it into a True key, so watch out when parsing workflows from scripts.

version: 1.10 comes out as 1.1.

Unquoted 1.10 is a number in YAML, and 1.10 equals 1.1. To keep a version string, quote it in the source YAML as "1.10".

What happens with a file containing several documents?

Each ----separated document becomes an array element in order, so a multi-resource file used with kubectl apply -f yields an array of resources.