YAML to JSON converter
Convert YAML to JSON or back, with line-accurate errors. Great for Kubernetes, Docker Compose and CI configs.
This tool runs in your browser. We never send what you type or paste to a server.
YAML gotchas
- Indentation is meaningful, and tabs are not allowed. Use spaces.
- The "Norway problem": in older YAML 1.1 parsers, unquoted
no,yes,onandoffbecome booleans. This converter uses the YAML 1.2 core schema, where onlytrueandfalseare booleans, but other tools may differ. Quote values to be safe. - Leading zeros:
010may be read as a number; quote version numbers and postcodes. - Multiple documents separated by
---are converted to a JSON array.
Library
Conversion uses the open-source js-yaml library.
Example
name: web
ports:
- 8080
release: 2024-01-15
converts to
{"name": "web", "ports": [8080], "release": "2024-01-15"}
Dates stay as plain strings, which matches what most applications expect.
How YAML maps onto JSON
Every YAML mapping becomes a JSON object, every sequence an array, and every scalar a string, number, boolean or null. Unquoted scalars are typed by the YAML 1.2 core schema: true/false (also True, TRUE), null, ~ or an empty value, and integers and floats. Anything else, including dates, stays a string. Quoting a scalar always makes it a string.
Comments, anchors, tags and the choice between quoted and plain styles are lost in JSON. Converting JSON back to YAML therefore gives the same data, not your original file.
Multi-line strings
Block scalars are common in CI files and Kubernetes ConfigMaps. | keeps line breaks, > folds them into spaces, and a trailing - removes the final newline:
run: |
npm ci
npm test
note: >-
one long
sentence
converts to
{"run": "npm ci\nnpm test\n", "note": "one long sentence"}
Anchors, aliases and merge keys
An anchor (&name) labels a value and an alias (*name) repeats it. JSON has no references, so each alias is written out in full:
base: &base {retries: 3, timeout: 30}
api: *base
gives {"base": {"retries": 3, "timeout": 30}, "api": {"retries": 3, "timeout": 30}}.
The merge key <<: *base, used heavily in Docker Compose and GitLab CI files, comes from YAML 1.1 and is not part of the 1.2 core schema. This converter merges it the way Docker Compose, PyYAML and most CI systems do: keys written next to << win over the merged ones, so api: {<<: *base, timeout: 60} gives {"retries": 3, "timeout": 60}.
Numbers that change
version: 1.10is a float, so it becomes1.1. Quote versions:version: "1.10".zip: 01234becomes1234here; a YAML 1.1 parser may read it as octal and give 668. Quote postcodes, phone numbers and IDs with leading zeros..infand.nanare valid YAML floats, but JSON cannot represent them, so they come out asnull.- Integers longer than 15 or 16 digits lose precision, as in any JavaScript tool. Quote them.
Converting YAML in code
These need a YAML library, which is not available on our test machine, so they were not run here; they follow each project's documentation.
Python with PyYAML (pip install pyyaml). PyYAML follows YAML 1.1, so unquoted no becomes False, and dates become datetime.date objects that json.dumps rejects without default=str:
python -c "import sys, json, yaml; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, default=str))" < config.yaml
Node.js with js-yaml (npm install js-yaml), the library this page uses:
import { readFileSync } from 'node:fs';
import yaml from 'js-yaml';
const data = yaml.load(readFileSync('config.yaml', 'utf8'), { schema: yaml.CORE_SCHEMA });
console.log(JSON.stringify(data, null, 2));
Command line with mikefarah's yq (version 4): yq -o=json config.yaml converts to JSON, and yq -p=json -o=yaml data.json goes the other way.
Questions
Is JSON valid YAML?
Almost always, yes. YAML 1.2 is a superset of JSON, so most JSON documents are also valid YAML.
Are YAML comments kept?
No. JSON has no comments, so comments are dropped when converting to JSON.
Why did "no" become false?
Older YAML (1.1) treated yes, no, on and off as booleans. This converter follows YAML 1.2, where only true and false are booleans, but some tools you use may still follow 1.1. Quote such values to be safe.
Why is my JSON output an array?
The YAML contained several documents separated by --- lines (common in Kubernetes manifests). Each document becomes one item of the array. If you expected a single object, look for a stray --- in the file.
Why does the converter say tabs are not allowed?
YAML forbids tab characters for indentation, because their width is ambiguous. Replace them with spaces; most editors can convert indentation for you. Tabs inside quoted strings are fine.
Can I convert a docker-compose.yml or Kubernetes manifest?
Yes, and it is a quick way to check the structure a tool will see. Merge keys (<<) are merged as Docker Compose does. Watch for unquoted values like port mappings 22:22, which YAML 1.1 tools could read as a base-60 number.