YAML is a superset of JSON

Since YAML 1.2, every valid JSON document is also valid YAML — a YAML parser will read JSON unchanged. That's because both share the same core data model: mappings (objects), sequences (arrays), and scalars (strings, numbers, booleans, null). Converting YAML data to JSON is therefore just a matter of parsing the YAML into that shared model and re-serializing it as JSON.

# YAML                          // JSON
server:                         {
  host: localhost                 "server": {
  port: 8080                        "host": "localhost",
roles:                              "port": 8080
  - admin                         },
  - editor                        "roles": ["admin", "editor"]
                                }

What doesn't survive the conversion

The conversion is lossless for data but lossy for everything YAML offers beyond data. When you convert YAML to JSON, you lose:

  • Comments. JSON has no comment syntax, so every # line is dropped. This is the most common surprise when converting config files.
  • Anchors and aliases. &defaults / *defaults let YAML reuse a block; JSON has no references, so the converter inlines a full copy wherever the alias appeared. Data is preserved, the sharing is not.
  • Multiple documents. A YAML file can hold several documents separated by --- (common in Kubernetes manifests). JSON has no multi-document concept, so these are typically emitted as a JSON array, one element per document.
  • Custom tags and non-string keys. YAML allows tags like !!python/object and complex (non-string) mapping keys; JSON keys must be strings, so these don't translate.

The type-inference gotcha carries over

Because YAML infers types before JSON ever enters the picture, a value already became a boolean or number during the YAML parse. So code: NO in YAML converts to "code": false in JSON, and zip: 01234 may become "zip": 668 (octal) or 1234. If the JSON output looks wrong, the cause is almost always an unquoted YAML value — quote it at the source.

Converting in code

In Python:

import yaml, json
data = yaml.safe_load(open("config.yaml"))
print(json.dumps(data, indent=2))

In JavaScript with js-yaml:

import yaml from "js-yaml";
const data = yaml.load(yamlString);
const json = JSON.stringify(data, null, 2);

Going the other way — JSON to YAML — is yaml.dump(json.load(...)) in Python or yaml.dump(JSON.parse(...)) in JS. Since JSON has no comments or anchors to begin with, JSON→YAML is fully lossless.

Convert YAML to JSON now

Paste YAML and get clean, formatted JSON instantly — or convert to XML. Runs in your browser, nothing uploaded.

Open YAML Converter →

Frequently Asked Questions

Is converting YAML to JSON lossless?

For data, almost always — JSON's data model is a subset of YAML's, so YAML using only those types converts cleanly. What's lost is everything that isn't data: comments, anchors and aliases (which get expanded), custom tags, and the boundaries between multiple documents in one file.

Can every JSON file be read as YAML?

Yes. YAML 1.2 is a strict superset of JSON, so any valid JSON document is also valid YAML and a YAML parser loads it correctly. The reverse isn't true — YAML has features like comments and anchors that JSON cannot represent.

What happens to YAML anchors and aliases when converting to JSON?

They are expanded. Anchors (&name) and aliases (*name) let YAML reuse a block, but JSON has no references, so the converter inlines a full copy of the referenced value wherever the alias appeared. The data is preserved but the shared reference is gone.

Why did a value change type when I converted to JSON?

The type changed during the YAML parse, before JSON was produced. Unquoted values like NO, yes, or 01234 are inferred as booleans or numbers by YAML, and that inferred type carries into the JSON. Quote the value in the YAML source to keep it a string.