JSON and YAML: the same data model, different syntax
YAML 1.2 is a superset of JSON: every JSON document is also valid YAML, and both describe the same kinds of values (mappings, sequences, strings, numbers, booleans, null). Converting between them is therefore lossless for ordinary data. The differences are about syntax and typing rules, and that is where surprises come from.
{
"name": "api",
"ports": [8080, 9090],
"debug": false,
"owner": null
}name: api
ports:
- 8080
- 9090
debug: false
owner: nullWhat to watch for when converting YAML to JSON
- Implicit typing. In YAML,
version: 1.10is the number 1.1,zip: 01234is a number in some parsers, andon,yes,nowere booleans in YAML 1.1. This converter uses the YAML 1.2 core schema (via js-yaml's default), soyesandnostay strings. Quote values you want to keep as strings:version: "1.10". - Dates. An unquoted
2026-02-01is parsed as a timestamp and serialised to JSON as an ISO 8601 string ("2026-02-01T00:00:00.000Z"). Quote it in the YAML if you want the literal text. - Anchors and aliases.
&default/*defaultare resolved: the aliased content is copied into each place it is used, because JSON has no references. - Multiple documents. A file with
---separators becomes a JSON array with one element per document. - Comments are dropped. JSON cannot carry them.
- Key order is preserved. Duplicate keys are an error in YAML, and this converter reports them.
What to watch for when converting JSON to YAML
- Strings that look like other types are quoted automatically (
"true","123","2026-02-01","null"), so round-tripping keeps them as strings. - Multi-line strings use a literal block (
|) which is easier to read than escaped\nsequences. - Long lines are not folded. Some YAML emitters wrap at 80 columns; that is legal but makes diffs noisy, so it is disabled here.
- Empty objects and arrays become
{}and[].
Converting in code
Python
import json, yaml # pip install pyyaml
data = yaml.safe_load(open("config.yaml"))
print(json.dumps(data, indent=2))
# JSON -> YAML
print(yaml.safe_dump(json.load(open("data.json")), sort_keys=False))
Node.js
import yaml from "js-yaml"; // npm install js-yaml
const data = yaml.load(await fs.readFile("config.yaml", "utf8"));
console.log(JSON.stringify(data, null, 2));
console.log(yaml.dump(JSON.parse(text), { noRefs: true }));
Command line
yq -o=json . config.yaml # mikefarah/yq
yq -P . data.json # JSON -> YAML (pretty)
Notes on this implementation
Conversion runs in your browser using the js-yaml library (MIT licence), loaded only on this page. JSON parsing uses the same diagnosing parser as the JSON Validator, so syntax errors come with line, column and a hint. YAML errors report the line and column from the YAML parser.