Example
# GitHub Actions workflow
name: CI
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
{
"name": "CI",
"on": {
"push": {
"branches": [
"main"
]
}
},
"jobs": {
"test": {
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v4"
},
{
"run": "npm ci && npm test"
}
]
}
}
}
Anchors and merge keys
YAML can define a block once with an anchor (&defaults) and reuse it with an alias
(*defaults). The merge key << copies its keys into another mapping. JSON has no
such feature, so the converter expands everything:
defaults: &defaults
adapter: postgres
pool: 5
production:
<<: *defaults
database: app_prod
{
"defaults": {
"adapter": "postgres",
"pool": 5
},
"production": {
"adapter": "postgres",
"pool": 5,
"database": "app_prod"
}
}
Common YAML errors and how to fix them
- Tabs for indentation: YAML only allows spaces. Replace tabs with two spaces.
- Inconsistent indentation: items of the same list or mapping must line up exactly.
-
Unquoted colons:
title: Note: read meis ambiguous. Quote the value:title: "Note: read me". -
Special characters at the start: values beginning with
*,&,!,@or a backtick must be quoted. - Duplicate keys: the same key twice in one mapping is an error in YAML 1.2.
Convert YAML to JSON in code
# yq
yq -o=json '.' config.yaml
# Python (PyYAML)
python -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))' < config.yaml
# Node.js (npm install yaml)
node -e 'const fs=require("fs"),YAML=require("yaml");console.log(JSON.stringify(YAML.parse(fs.readFileSync(0,"utf8")),null,2))' < config.yaml
Going the other way? Use JSON to YAML.
Frequently asked questions
How do I convert YAML to JSON?
Paste YAML into the tool above, or drop a .yaml or .yml file onto it. The JSON appears as you type, with 2- or 4-space indentation. If the YAML has a syntax error, the exact line and column are shown.
Does this support anchors, aliases and merge keys?
Yes. Anchors (&name) and aliases (*name) are expanded, and merge keys (<<: *name) are applied. That is how Docker Compose files, GitLab CI and Rails database.yml reuse configuration.
What happens with multiple YAML documents?
A file with several documents separated by --- (common in Kubernetes manifests) becomes a JSON array with one element per document.
Why is "on" a key in my GitHub Actions JSON and not true?
This converter follows YAML 1.2, the current spec, where only true and false are booleans. YAML 1.1 parsers turn on, off, yes and no into booleans too, which famously makes GitHub Actions’ on: key become true in some tools.
Are YAML comments kept?
No. JSON has no comment syntax, so comments are dropped during conversion. Keep the YAML as your source of truth if the comments matter.