How YAML maps to JSON
YAML mappings become JSON objects, sequences become arrays, and scalars become strings, numbers, booleans or null according to the YAML 1.2 core schema. A few YAML features have no JSON equivalent and are converted as follows:
| YAML feature | In JSON |
|---|---|
Comments (# …) | Dropped |
Anchors and aliases (&base, *base) | Resolved into copies of the value |
Merge keys (<<: *base) | Keys merged into the mapping |
Several documents separated by --- | One array with one item per document |
Block scalars (|, >) | Strings with the newlines YAML defines |
Non-string keys (1: a, true: b) | String keys ("1", "true") |
.inf, .nan | null, with a warning |
How to convert YAML to JSON
- Paste the YAML or open a
.yaml/.ymlfile. Syntax errors are listed with their line numbers. - Choose the JSON indentation (2 spaces, 4 spaces or tab) and whether to sort keys.
- Click To JSON. Use Minify instead for single-line JSON.
Example: anchors and merge keys
defaults: &defaults
retries: 3
timeout: 30
production:
<<: *defaults
timeout: 60
converts to
{
"defaults": {"retries": 3, "timeout": 30},
"production": {"retries": 3, "timeout": 60}
}
(shown compactly here). The explicit timeout: 60 wins over the merged value.
Exact numbers
Integers are parsed as arbitrary-precision values, so an ID such as 12345678901234567890 appears in the JSON exactly as written. Many converters turn it into 12345678901234567000 because they go through JavaScript numbers.
To go the other way use JSON to YAML; to check the YAML first use the YAML validator.