How to convert YAML to JSON
Updated · 3 min read
YAML is pleasant to write but easy to get subtly wrong: one misplaced space changes the structure. Converting it to JSON shows exactly how a parser understands your file, and gives you JSON for APIs, JavaScript code or tools that do not read YAML. The converter follows the YAML 1.2 core schema used by modern tooling.
- Paste YAML or drop a
.yaml/.ymlfile. - If the YAML is invalid, the error message shows the line where parsing failed.
- Pick an indentation for the JSON output under Options.
- Copy the JSON or download it.
Example
YAML input
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: web
spec:
replicas: 3
template:
spec:
containers:
- name: nginx
image: nginx:1.27
ports:
- containerPort: 80JSON output
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "web",
"labels": {
"app": "web"
}
},
"spec": {
"replicas": 3,
"template": {
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:1.27",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}A Kubernetes manifest becomes nested JSON objects and arrays — exactly the structure the Kubernetes API itself receives, since kubectl converts YAML to JSON before sending it.
Your data stays on your device. The conversion runs in JavaScript inside this tab — files and pasted text are never uploaded to DataSwitch Pro.
YAML rules used by the converter
- YAML 1.2 core schema: only
true/falseare booleans. Words such asyes,no,onandoffstay strings, avoiding the classic YAML 1.1 surprises. - Dates stay strings such as
"2026-10-31"rather than being converted to timestamps. - Anchors and aliases (
&base,*base) and merge keys are expanded into full JSON. - Multi-document files separated by
---become a JSON array with one element per document.
Common YAML errors
- Tabs for indentation — YAML only allows spaces.
- Inconsistent indentation between sibling keys.
- Missing space after a colon:
key:valueis a single string, not a key/value pair. - Unquoted special characters: values starting with
@,`,*,&,!,%or containing:or#need quotes. - Leading zeros: an unquoted
01234is read as the number 1234 — quote ZIP codes and IDs.
JSON as a YAML debugger
When a deployment or pipeline behaves strangely, converting its YAML to JSON is one of the fastest ways to check the structure. Lists that should be nested but are not, keys that ended up at the wrong level, or a value that became a number instead of a string are immediately visible in the JSON.
Real-world examples
Check what a GitHub Actions workflow really contains
Indentation mistakes in YAML change the meaning silently. The JSON view shows whether steps really sits under the right job.
YAML
name: CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm testJSON
{
"name": "CI",
"on": [
"push"
],
"jobs": {
"test": {
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v4"
},
{
"run": "npm test"
}
]
}
}
}Feed configuration to a JSON-only API
Many APIs and CLIs accept JSON only. Write the settings comfortably in YAML, with anchors to avoid repetition, and convert before sending.
YAML
defaults: &defaults
timeout: 30
retries: 3
staging:
<<: *defaults
url: https://staging.example.com
production:
<<: *defaults
url: https://example.com
retries: 5JSON
{
"defaults": {
"timeout": 30,
"retries": 3
},
"staging": {
"timeout": 30,
"retries": 3,
"url": "https://staging.example.com"
},
"production": {
"timeout": 30,
"retries": 5,
"url": "https://example.com"
}
}Common errors and how to fix them
"bad indentation of a mapping entry"
A key is indented with a different number of spaces than its siblings. Align it with the keys at the same level — the error shows the line number.
"end of the stream or a document separator is expected"
Usually a missing space after a colon or a stray character at the end of a line. Check the line reported in the error.
A version like 1.10 became 1.1
Unquoted, 1.10 is a number. Quote it — "1.10" — to keep it as text.
on: became true in other tools
YAML 1.1 parsers read on as a boolean. This converter uses YAML 1.2, where it stays the string "on", matching what GitHub Actions expects.
Related guides
Convert YAML to JSON in code
For one-off files the tool above is quickest. To automate the same task in a script or pipeline, these snippets are a good starting point.
Python (PyYAML)
import json
import yaml # pip install pyyaml
with open("config.yaml", encoding="utf-8") as f:
data = yaml.safe_load(f)
print(json.dumps(data, indent=2, ensure_ascii=False, default=str))
Command line (yq)
yq -o json '.' config.yaml > config.json
Node.js (js-yaml)
import yaml from "js-yaml"; // npm install js-yaml
const json = JSON.stringify(yaml.load(text), null, 2);
Frequently asked questions
Why did yes/no not become true/false?
The converter follows YAML 1.2, where only true and false are booleans. Older YAML 1.1 parsers treat yes/no/on/off as booleans, which causes bugs such as the country code NO turning into false.
Can it convert files with multiple documents?
Yes. Documents separated by --- are returned as a JSON array, in order.
Are comments kept?
No. JSON has no comment syntax, so YAML comments are dropped.
What happens to anchors and aliases?
They are resolved, so the JSON contains the full repeated content.
Is my YAML uploaded?
No. Parsing happens in your browser, so configuration containing secrets stays on your device.