JSON guide · 2 min read
JSON and YAML Conversion: What to Check Before and After
Convert between JSON and YAML safely while checking scalar types, comments, anchors, duplicate keys and round-trip behavior.
The two formats overlap, but YAML has more source features
YAML can represent the mappings, sequences and scalar values used by ordinary JSON data. It also supports comments, anchors, aliases, tags and other syntax that standard JSON cannot preserve directly.
JSON-to-YAML conversion is therefore usually straightforward at the data level, while YAML-to-JSON can lose source-level information even when the resulting values remain equivalent.
Watch implicit scalar interpretation
Depending on YAML version and parser, unquoted values may be interpreted as numbers, booleans, nulls or other scalar types. Quote identifiers whose exact textual representation must be preserved.
account_id: "00123"
enabled_text: "true"Comments do not survive standard JSON
JSON has no comment syntax. If YAML comments contain operational instructions or documentation, converting to JSON and back cannot recreate them. Keep the original YAML source when comments are part of the maintained configuration.
Anchors and aliases are resolved or lost
YAML anchors can reuse values. JSON has no equivalent reference syntax, so conversion normally expands or resolves them. A round trip can therefore produce more verbose YAML even when application behavior is unchanged.
Reject ambiguous duplicate keys
Some parsers accept duplicate mapping keys and choose one value. Treat duplicate keys as errors in configuration pipelines so conversion does not silently select a value you did not intend.
Validate the converted data model
Parse the source, convert it, inspect important types and nested structures, then test it with the consuming application or schema. Visual similarity is not enough for configuration where a small type change can alter runtime behavior.
- Check leading-zero identifiers.
- Confirm Boolean, numeric and null types.
- Expect comments and source formatting to be lost.
- Review anchors and aliases before expecting reversible conversion.
- Validate with the actual consumer.
Common questions
Frequently asked questions
Will YAML comments survive conversion to JSON?
No. Standard JSON has no comment representation, so keep the original YAML when its comments matter.
Can valid JSON be represented in YAML?
Yes at the data level for ordinary JSON values, though source formatting and presentation will differ.
Why can a YAML-to-JSON round trip change the YAML file?
Comments, anchors, aliases and formatting can be lost or expanded because JSON has no direct representation for them.