Guide

How to convert YAML to JSON

YAML-to-JSON conversion commits to the parser’s interpretation of each value. An unquoted version such as 1.10 becomes a number, while a quoted version remains a string. Comments and source notation disappear because the result is serialized data rather than a copy of the YAML document.

This processor uses the yaml library with YAML 1.2 core defaults, explicitly enables merge-key expansion, and sets an alias-expansion limit. It parses one document. Those details matter more than broad claims about what YAML parsers usually do: a different parser or an explicit version directive can change the answer.

Open the YAML to JSON

Use this when


  • An API or library needs JSON and your configuration is YAML
  • You want to inspect the types a YAML parser actually produced
  • A config needs JSON Schema validation or processing with jq
  • You are checking anchors, merge overrides or ambiguous scalar values
  • A pipeline requires one explicit JSON value rather than YAML authoring syntax

Type inference is the whole story


Under the default YAML 1.2 core schema, true and false are booleans, null and a tilde represent null, and numeric forms become numbers. Bare yes, no, on and off remain strings. ISO-looking dates normally remain strings too. The rule is not “everything without quotes is a string except digits”; YAML has a defined schema with several scalar spellings.

Quote identifiers and versions when their spelling is data. The default parser reads 0755 as decimal 755, whereas 0o755 is octal 493. A version written as 1.10 becomes 1.1. The converter cannot recover whether that number once had a trailing zero, so fix the YAML before conversion.

The result also passes through JavaScript values and JSON.stringify. Huge integers and precise decimals can round. YAML non-finite numbers such as .inf and .nan become JSON null, because JSON serialization has no finite numeric representation for them. Ordinary structural comparisons cannot recover those original scalars.

country: NO
enabled: on
version: 1.10
code: "0755"
mode: 0755
permissions: 0o755
Paste this single document with no version directive to exercise the default YAML 1.2 core rules.
{
  "country": "NO",
  "enabled": "on",
  "version": 1.1,
  "code": "0755",
  "mode": 755,
  "permissions": 493
}
Expected output with the default two-space JSON indent. The quoted code survives; the unquoted version and mode are numbers.
Input scalarDefault JSON valueReview decision
yes / no / on / offstringOther YAML versions may disagree
"1.10" / 1.10"1.10" / 1.1Quote versions
.inf / .nannullReplace unsupported numbers deliberately
0755 / 0o755755 / 493Quote identifiers; distinguish bases

What has no JSON equivalent


Comments, anchor names, formatting style and tag syntax are not represented in JSON. Supported tags influence the parsed value before their syntax disappears; this converter does not register application-specific custom tag handlers. Keep the YAML source if comments or tags document something beyond the resulting value.

Ordinary aliases reuse a parsed value, which JSON serialization writes out at each use. This can make the result larger. Alias expansion is limited with maxAliasCount set to 100; that is a library protection against excessive expansion, not a promise that any document containing fewer than one hundred visible aliases will succeed.

A cyclic alias graph cannot be serialized as JSON and is rejected. Non-string mapping keys must become object property names, so their type identity is not preserved. The processor’s non-string-key warning is heuristic: it looks at numeric or boolean-looking property names after conversion and is not proof that every problematic source key was detected.

Multi-document files


A YAML stream can contain several documents separated by document markers. A single leading --- is normal and does not make a stream multi-document; another actual document is what matters. Kubernetes bundles commonly contain multiple resources this way.

The converter rejects multiple documents through the library’s single-document parser. It does not take the first document, even though an old internal warning string mentions that possibility. There is no option to automatically return an array of documents.

Split resources deliberately and convert each separately. If the target expects one array, assemble a YAML sequence or JSON array with that agreed schema. Do not blindly split every line beginning with dashes: use a proper YAML-stream parser for automated processing, and keep document boundaries separate from content inside scalars.

---
name: first
---
name: second
Expected result: Invalid YAML, not a partial conversion. Submit each document separately.

Duplicate keys and merge keys


The library rejects duplicate mapping keys by default. Repeating timeout twice in one mapping is an error, not a last-value-wins override. Correct the source so each local key appears once; a failed parse produces no JSON to compare.

Merge keys are different. The << syntax is a YAML 1.1 extension, not part of the YAML 1.2 core schema. This processor explicitly sets merge: true, so supported merges are expanded, with local properties overriding merged defaults. That behavior is a configured feature here, not a universal rule to assume in another YAML 1.2 parser.

A merge source can supply several defaults, which can obscure where the final setting came from. Review the referenced mapping and local overrides together. The resulting JSON contains values, not provenance: after conversion you cannot tell whether retries was inherited or written locally.

defaults: &base
  retries: 2
  enabled: true
job:
  <<: *base
  retries: 5
Configured merge example: job becomes {"retries":5,"enabled":true}; defaults remains its own object. Member order is not the comparison target.
timeout: 10
timeout: 30
Expected result: Invalid YAML for duplicate mapping keys. This is not the same operation as overriding a merged default.

Verifying the conversion


Directives can affect interpretation before they disappear. An explicit %YAML 1.1 directive selects YAML 1.1 behavior, so bare NO can become false even though it is a string under the usual defaults. %TAG establishes tag-handle notation; it does not add a custom application tag implementation to this converter.

The processor adds its directive warning when it sees a %YAML line. A %TAG-only document does not necessarily trigger that warning. Neither directive has a JSON representation, so absence of a warning is not evidence that all YAML authoring information survived.

Verify the output types, not just whether conversion completed. Check quoted identifiers, nulls, version strings and expected booleans. Validate against a schema or the destination application for required fields and allowed values; syntax conversion cannot decide whether the configuration is useful or safe for that application.

%YAML 1.1
---
country: NO
Unlike the default example, this explicitly versioned document produces {"country":false} and a directive warning.
  • Confirm the intended YAML version and inspect directives.
  • Resolve duplicate-key and multi-document errors in the source.
  • Check types and values after merge expansion.
  • Keep the original YAML when comments, aliases or tags matter.
  • Validate the JSON against the destination schema.

YAML 1.2.2 specification — Primary reference for documents, directives and core scalar resolution.

yaml library parsing options — Library behavior for version selection, duplicate keys, merges and alias limits.

Common problems


Each of these is something that actually happens, with the cause rather than a generic suggestion to check your input.

A version number became 1.1

Cause
The YAML source wrote 1.10 as a number rather than a quoted string.
Fix
Quote the original version and convert again; the output cannot reconstruct its spelling.

All comments are gone

Cause
JSON has no comment representation.
Fix
Keep YAML as the authoring source and generate JSON when needed.

Alias conversion failed or the JSON grew substantially

Cause
JSON repeats aliased values; excessive expansion is limited and cycles cannot be serialized.
Fix
Reduce the shared structure or provide an acyclic JSON-compatible model; do not bypass the limit blindly.

A resource bundle is rejected

Cause
The single-document parser rejects multiple YAML documents.
Fix
Convert each resource separately or deliberately build the one array expected by the destination.

A local setting differs from its defaults block

Cause
Configured merge expansion lets a local key override the merged value.
Fix
Inspect the local mapping and merge source together. A repeated local key is instead an error.

Questions


Is converting YAML to JSON lossless?
No general guarantee applies. Comments, aliases and directives are not retained as syntax; values depend on schema and version, large numbers can round, and non-finite numbers become null. Multi-document input and duplicate keys are rejected.
Why did a string turn into a number?
It was an unquoted scalar matching the active YAML numeric rules. Quote it in the YAML source when its spelling or string type matters.
What happens to anchors and aliases?
Ordinary acyclic aliases expand into repeated JSON values. Merge expansion is explicitly enabled here; excessive alias expansion and cycles can fail rather than produce output.
Can I convert several Kubernetes resources at once?
Not as a multi-document stream in this tool. Split and convert resources separately, or deliberately provide an array if the consumer expects one.