What changes when you convert YAML to JSON
YAML and JSON share one data model, so values survive and only the notation and comments are dropped. Two things still deserve a look afterwards: strings that look like numbers and dates.
Same values, different notation
A YAML mapping becomes a JSON object, a sequence becomes an array, and a scalar becomes a string, number, boolean or null. Because both formats use the same data model, nothing is restructured.
What changes is notation. Comments, anchor names, tags and indentation have no place in JSON and disappear.
| YAML | JSON | Result |
|---|---|---|
| mapping a: 1 | {"a": 1} | unchanged |
| sequence - x | ["x"] | unchanged |
| anchor &b, alias *b | values copied | only the name is lost |
| comment # ... | none | removed |
| block scalar | | "line1\nline2\n" | one string |
Comments and anchors are not values
In the YAML below, &defaults is an anchor name and << is a merge key. Converted to JSON, only the merged result remains; the anchor name is gone.
A merge key never overwrites a key that is already present, so an explicit value always wins.
defaults: &defaults
retries: 3
timeout: 30
service:
<<: *defaults
timeout: 60
=> {"defaults":{"retries":3,"timeout":30},
"service":{"retries":3,"timeout":60}}
Three things to check afterwards
First, an unquoted 007 or 1.10 may be read as a number. Version strings, build codes and phone numbers should be quoted.
Second, date spelling is interpreted differently by different tools. Third, integers above 2^53 cannot be held exactly by a JSON number, so keep them as strings.
- Versions and codes: quote them, "1.10"
- Dates: settle on ISO 8601 strings
- Large integers: keep as decimal strings
Questions
Can I keep YAML comments in JSON?
JSON has no comment syntax. If you must preserve them, keep the file in YAML or move the note into a key such as x-note.
Why does the key order change?
A YAML mapping does not promise order. This tool keeps the order you typed, but other tools may sort keys. Use an array when order matters.
Sources
Every example here can be pasted into the converter as it is.
Open the converter