Convert YAML to JSON: What Changes, What Gets Lost, and When to Keep YAML
A practical review of YAML-to-JSON conversion: how it works, which details are lost, how YAML type rules can change values, and how to check the result before you use it.
Converting YAML to JSON is usually simple. Most configuration files, API payloads and data fixtures come through with their meaning intact. Problems tend to appear in a few predictable places: comments, YAML's loose typing of unquoted values, and YAML features that JSON has no way to express. This review explains what the conversion changes, where it can quietly alter data, and when the YAML original is the better file to keep.
A note on scope
This is not a visual text effect. YAML-to-JSON conversion does not reverse text, mirror it, swap in look-alike Unicode characters or change how text is displayed. It parses one data format and writes the same data in another. The questions below apply to conversion only. No specific online converter or library version was tested for this article, so tool-specific behavior is described as documented or typical and should be confirmed against the tool you use.
Is converting YAML to JSON right for your use?
Conversion is worth doing when a program, API or validator expects JSON and your data starts as YAML. JSON has one strict syntax and is supported almost everywhere. That makes it a dependable format for exchanging data between programs.
The main cost is information loss. YAML can hold comments, reusable anchors, explicit type tags and several documents in one file. JSON can hold none of these. The values themselves usually survive, but the notes people left for each other do not, and some values can change type.
Keep YAML as the file you edit when people maintain it by hand, rely on comments to explain settings, or use anchors to avoid repeating blocks. In that situation, generate JSON from the YAML when a tool needs it. Do not replace the YAML file with the JSON output.
What the conversion actually changes
A converter does not rewrite YAML text line by line. It first parses the YAML into a data structure made of mappings (key-value pairs), sequences (lists) and scalars (single values such as strings, numbers, booleans and null). It then writes that structure out as JSON. Anything that is not part of the data structure is dropped. Anything JSON cannot represent has to be translated, rejected or handled in a way specific to the tool.
| YAML feature | What happens in JSON |
| Comments (# ...) | Removed; JSON has no comment syntax |
| Anchors and aliases (&name, *name) | Expanded into full copies of the referenced data |
| Merge keys (<<) | Merged into the mapping if the parser supports them; parser support varies |
| Multiple documents (--- separators) | Need special handling; tools may output only the first document, an array or separate JSON values |
| Block scalars (| and >) | Become ordinary strings with escaped newlines; the layout style is lost |
| Explicit tags (!!binary, custom tags) | Converted, rejected or turned into strings, depending on the tool |
| Non-string keys (numbers, booleans, complex keys) | JSON keys must be strings, so keys are converted or the conversion fails |
| Dates and timestamps | JSON has no date type; values become strings or cause an error |
| Infinity and NaN (.inf, .nan) | Not valid in standard JSON; may be rejected or written as non-standard output |
| Key order | Usually kept by common serializers, but the JSON specification does not guarantee object order |
Type surprises from unquoted values
Meaning is most often damaged by implicit typing. YAML decides the type of an unquoted value by its spelling, and the rules depend on which version of YAML the parser follows.
- YAML 1.1 parsers read yes, no, on and off (in several capitalizations) as booleans. A country code such as NO can become false.
- A version number such as 1.10 is read as the float 1.1, which loses the trailing zero.
- Values with a leading zero, such as 0755, may be read as octal numbers under YAML 1.1 rules.
- Date-like values such as 2024-05-01 may be parsed as dates, which JSON cannot hold directly.
- An empty value, null or ~ becomes JSON null, not an empty string.
The YAML 1.2 core schema is stricter. For example, it recognizes only true and false as booleans. However, many widely used parsers still apply YAML 1.1 rules, so you cannot assume one behavior everywhere. The dependable fix is in the source file: quote any value that must stay a string, such as version: "1.10" or country: "NO".
Worked example
Here is a short YAML input:
# Service settings
name: api
version: 1.10
debug: no
port: 8080
created: 2024-05-01
tags: [web, internal]
defaults: &defaults
retries: 3
staging:
<<: *defaults
region: us-east
A YAML 1.1 parser that supports merge keys, with dates written out as strings, would typically produce:
{
"name": "api",
"version": 1.1,
"debug": false,
"port": 8080,
"created": "2024-05-01",
"tags": ["web", "internal"],
"defaults": {
"retries": 3
},
"staging": {
"retries": 3,
"region": "us-east"
}
}
Compare the two:
• The comment is gone.
• The anchor has been expanded into a copy, so the link between defaults and staging no longer exists.
• version lost its trailing zero.
• debug became a boolean.
Under YAML 1.2 core rules, debug would stay the string "no" and created would stay a plain string. version would still become 1.1, because it is unquoted and looks like a number. This example shows the expected behavior from documented parsing rules. It is not a recorded run of a particular tool.
Converting on the command line
Two common local options are shown below. Check the documentation for your installed version, because flags and defaults change between releases. The first uses the Go-based yq tool. The second uses Python with the PyYAML library, which follows YAML 1.1 rules.
yq -o=json config.yaml
python3 -c 'import sys, json, yaml; print(json.dumps(yaml.safe_load(sys.stdin), indent=2, default=str))' < config.yaml
In Python, use safe_load rather than an unrestricted loader. YAML tags can tell an unrestricted loader to build arbitrary Python objects, which is a security risk with untrusted input. The default=str argument turns dates into strings. Without it, the json module raises an error when it reaches a date value. safe_load reads only the first document; multi-document files need safe_load_all.
Will the JSON stay readable and usable after copying or sharing?
JSON is plain text, so the output copies and pastes like any other text. It contains no styled glyphs, no substituted look-alike characters and no direction-control tricks. Screen readers and search tools treat it as ordinary text, although reading the braces, brackets and quotation marks aloud is slow. When people need to read the content, a plain-language summary or the commented YAML is often more useful than raw JSON.
- Pretty-print the JSON with indentation if people will read it. Minified JSON is valid but hard to scan.
- Avoid pasting JSON through word processors or chat tools that turn straight quotes into curly quotes. Curly quotes make the JSON invalid.
- Some serializers write non-ASCII characters as \u escape sequences. The data is unchanged, but the text is harder to read; most tools have an option to keep the original characters.
- JSON has no comments, so put any explanation for readers in a separate document or field, not in the file itself.
- If the JSON will be edited by hand and converted back to YAML, expect comments and anchors to be gone for good.
Privacy when using online converters
Configuration files often contain credentials, internal hostnames or tokens. A converter's appearance or marketing tells you nothing reliable about whether it stores what you paste. Its documented policy is the only guide, and even that is something you take on trust. For sensitive files, use a local tool such as one of the commands above, or remove secrets before pasting.
Checking that the conversion kept your meaning
- Run the output through a JSON validator to confirm the syntax is valid.
- Check the type of each sensitive value: version strings, country codes, yes/no flags, values with leading zeros and dates.
- If the file has several documents, confirm how the tool handled the --- separators.
- If a JSON Schema exists for the target system, validate the output against it.
- For repeated or automated conversions, keep a small test file of known edge cases and compare each new output with the expected JSON.
Evidence gaps
No specific converter, library version or destination system was tested for this article. The behavior described comes from the YAML and JSON specifications and from commonly documented parser behavior. How a particular tool handles merge keys, tags, multiple documents, duplicate keys and dates should be confirmed with a test on that tool.