Why convert YAML to JSON
YAML is pleasant to write by hand, but most programs want JSON. Common reasons to go in this direction:
- Feeding a config file to a REST API,
jqor a JSON Schema validator. - Checking what a tricky file means: indentation, quoting and block scalars are easy to misread, and JSON shows resolved values with no ambiguity.
- Storing a Helm values file or an OpenAPI spec in a system that only accepts JSON.
If you are deciding which format to keep long-term, the JSON vs YAML guide compares them.
How the YAML is read
The converter parses YAML 1.2 (the core schema) in strict mode and then writes standard JSON. That choice decides how plain, unquoted values turn into types:
trueandfalsebecome booleans,nulland~become null. Words likeyes,no,onandoffstay strings, because only YAML 1.1 readers treat them as booleans.- Integers and floats become JSON numbers.
0x1Fis read as hex (31) and0o755as octal (493);0755without theois plain decimal 755. - Unquoted dates such as
2024-01-01stay strings, since YAML 1.2 has no timestamp type. - Integers larger than 2^53 are written with all their digits instead of being rounded.
.infand.nanhave no JSON spelling and come out as null.- Literal blocks (
|) keep their line breaks; folded blocks (>) join lines with spaces.
Keys keep their order, and a file with several documents separated by --- becomes a JSON array with one element per document.
When your tool reads YAML differently
Much of the YAML in the wild is consumed by YAML 1.1 parsers, and the differences show up in exactly the values people forget to quote:
- File modes in Kubernetes.
defaultMode: 0440is octal to Kubernetes, which is why its documentation warns that JSON has no octal literals and that a JSON manifest needs the decimal value. Here it converts to440, which is not the same as octal 0440 (decimal 288). If you are producing a JSON manifest, write288, or write0o440in the YAML first. - Unquoted
yesandonin Kubernetes. A ConfigMap’sdatavalues must be strings, and Kubernetes tooling reads an unquotedonas a boolean, then fails withcannot unmarshal bool into Go struct field. This page shows it as the string"on", so it will look fine here. Quote those values. - The
on:key in GitHub Actions. PyYAML reads the key as the booleanTrue, which surprises anyone scripting over workflow files. Under YAML 1.2 it stays the key"on".
Treat the output as the YAML 1.2 reading, and check the cases above against what your consumer does.
Anchors, aliases and merge keys
An anchor (&base) and its aliases (*base) are expanded: every alias is replaced by a full copy of the anchored value. To protect your browser from “billion laughs” files, expansion stops after 1,000 aliases and the conversion fails with an explanation instead of hanging.
The << merge key is a YAML 1.1 extension and is not applied. It survives as an ordinary key named "<<" that contains the copied mapping. If your tooling relies on merges (Docker Compose and GitLab CI do), check those keys in the output and inline the merged fields where needed.
What does not survive
JSON has nowhere to put comments, so every # comment is dropped and an info note says so. Tags such as !!str are applied and then disappear, and custom application tags lose their meaning. Anchors and aliases vanish too, replaced by the duplicated data, so the JSON can be noticeably longer than the YAML it came from.
There are no conversion options to set. Errors are reported with a line and column, and nothing is guessed: duplicate keys and tab indentation stop the conversion rather than silently picking a value. To go back the other way, use the JSON to YAML converter; to tidy the YAML first, run it through the YAML formatter.
Examples
Kubernetes Deployment with a Secret volume
Flow mappings expand and the quoted “true” stays a string. Look at defaultMode: YAML 1.2 gives decimal 440, whereas Kubernetes treats 0440 in YAML as octal, so a JSON manifest needs 288.
apiVersion: apps/v1
kind: Deployment
metadata:
name: payments-api
namespace: prod
labels: {app: payments-api, tier: backend}
spec:
replicas: 3
selector:
matchLabels:
app: payments-api
template:
metadata:
labels:
app: payments-api
spec:
containers:
- name: api
image: registry.example.com/payments-api:2.14.1
ports:
- containerPort: 8443
env:
- name: LOG_LEVEL
value: info
- name: ENABLE_TRACING
value: "true"
resources:
limits: {cpu: 500m, memory: 512Mi}
volumes:
- name: tls
secret:
secretName: payments-tls
defaultMode: 0440
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "payments-api",
"namespace": "prod",
"labels": {
"app": "payments-api",
"tier": "backend"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": {
"app": "payments-api"
}
},
"template": {
"metadata": {
"labels": {
"app": "payments-api"
}
},
"spec": {
"containers": [
{
"name": "api",
"image": "registry.example.com/payments-api:2.14.1",
"ports": [
{
"containerPort": 8443
}
],
"env": [
{
"name": "LOG_LEVEL",
"value": "info"
},
{
"name": "ENABLE_TRACING",
"value": "true"
}
],
"resources": {
"limits": {
"cpu": "500m",
"memory": "512Mi"
}
}
}
],
"volumes": [
{
"name": "tls",
"secret": {
"secretName": "payments-tls",
"defaultMode": 440
}
}
]
}
}
}
}
GitHub Actions workflow
The on key stays a string under YAML 1.2, the empty pull_request trigger becomes null, and the literal block keeps its line breaks as \n.
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: |
npm ci
npm test -- --coverage
{
"name": "ci",
"on": {
"push": {
"branches": [
"main"
]
},
"pull_request": null
},
"jobs": {
"test": {
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v4"
},
{
"name": "Run tests",
"run": "npm ci\nnpm test -- --coverage\n"
}
]
}
}
}
Docker Compose anchors with a merge key
The aliases are expanded, but the merge is not applied: each service holds a literal “<<” key, which is different from what Docker Compose itself sees.
x-common: &common
restart: unless-stopped
environment:
TZ: Asia/Singapore
services:
api:
<<: *common
image: acme/api:1.8
worker:
<<: *common
image: acme/worker:1.8
command: ["node", "worker.js"]
{
"x-common": {
"restart": "unless-stopped",
"environment": {
"TZ": "Asia/Singapore"
}
},
"services": {
"api": {
"<<": {
"restart": "unless-stopped",
"environment": {
"TZ": "Asia/Singapore"
}
},
"image": "acme/api:1.8"
},
"worker": {
"<<": {
"restart": "unless-stopped",
"environment": {
"TZ": "Asia/Singapore"
}
},
"image": "acme/worker:1.8",
"command": [
"node",
"worker.js"
]
}
}
}
Plain scalars and their JSON types
Shows YAML 1.2 typing: yes and the date stay strings, 0755 is decimal, the huge build number keeps every digit and ~ becomes null.
enabled: yes
retries: 3
ratio: 0.75
mode: 0755
released: 2024-01-01
build: 12345678901234567890
empty: ~
{
"enabled": "yes",
"retries": 3,
"ratio": 0.75,
"mode": 755,
"released": "2024-01-01",
"build": 12345678901234567890,
"empty": null
}
Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Duplicate key — keys in a mapping must be uniqueExplained | The same key appears twice at one level of a mapping, often after copy-pasting a block. | Delete or rename one of the keys. JSON parsers disagree on which duplicate wins, so the converter refuses to guess. |
Tabs are not allowed for indentation in YAMLExplained | A line is indented with a tab character; YAML only accepts spaces for indentation. | Replace the tabs with spaces (two per level is usual). Most editors can convert indentation for the whole file. |
A nested block cannot be used as a keyExplained | A line such as a: b: c puts a second colon-space where YAML expects a plain value, for example in an unquoted command. kubectl reports this as: error converting YAML to JSON: yaml: mapping values are not allowed in this context. | Quote the value (a: “b: c”) or move the nested mapping onto its own indented line. |
All mapping items must start at the same columnExplained | One key in a block is indented differently from its siblings, often by a single space after a hand edit of a manifest. | Line the key up with the others at its level. Go-based tools report the same mistake as: did not find expected key. |
Plain value cannot start with reserved character @Explained | Unquoted values may not begin with @ or a backtick, which YAML reserves. | Wrap the value in quotes, for example handle: “@team”. |
Too many alias expansions — this looks like a "billion laughs" alias bomb, so it was not expandedExplained | Aliases refer to anchors that themselves contain aliases, multiplying into more than 1,000 expansions. | If the file is legitimate, reduce the nesting of aliases; if it came from an untrusted source, treat it as hostile. |
Frequently asked questions
Are YAML comments kept in the JSON?
No. JSON has no comment syntax, so comments are dropped and an info message tells you that happened. Keep the YAML as the source of truth if the comments matter.
Why did "no" or "off" stay a string instead of becoming false?
The converter follows YAML 1.2, where only true and false are booleans. Older YAML 1.1 parsers such as PyYAML would read no as false, which is the well-known Norway problem; write false explicitly if that is what you mean.
Why did my Kubernetes file mode 0644 turn into 644?
YAML 1.2 reads 0644 as the decimal number 644. Kubernetes reads it as octal, which is decimal 420, and JSON has no octal form. Use 420 in a JSON manifest.
What happens to a file with several documents?
All documents are converted and wrapped in a JSON array in their original order. A single document converts to its value directly, with no wrapping array.
Are very large numbers rounded?
Integers beyond JavaScript’s safe range are copied digit for digit into the JSON. Whether the program that later reads the JSON keeps them exact depends on its parser.
Is my YAML sent to a server?
No. Parsing and conversion happen in your browser, and the page makes no network request with your input.