YAML to JSON: the complete guide
The YAML format, the conversion to JSON, and how to handle real-world edge cases.
YAML syntax basics
YAML uses three structural markers: indentation for hierarchy, colons for key-value pairs, and dashes for list items. A simple document:
name: web-server
replicas: 3
image: nginx:1.25
ports:
- 80
- 443
labels:
app: web
tier: frontend
Two-space indent is the convention; three or four is legal but unusual. Tabs are forbidden in YAML — only spaces. The colon after a key must be followed by a space. A dash at the start of a line introduces a list item.
YAML is also a strict superset of JSON: any valid JSON document is also valid YAML. That's what makes the conversion lossless.
YAML's type system
YAML 1.2 has six core scalar types plus a few extras:
- Mappings (objects): key-value collections, written as nested indentation. Become JSON objects.
- Sequences (arrays): ordered lists, written with dashes. Become JSON arrays.
- Strings: either quoted (with single or double quotes) or plain (no quotes, when unambiguous). Become JSON strings.
- Integers: 42, -17, 0x2A (hex). Become JSON numbers.
- Floats: 3.14, 1.5e10, .inf, .nan. Become JSON numbers (except .nan which becomes a string).
- Booleans: true / false (YAML 1.2 canonical). Yes / no / on / off are YAML 1.1 and not supported in 1.2. Become JSON booleans.
- Null: null, ~, or empty. Become JSON null.
When parsing, the type is inferred based on the value's syntax. A string that looks like a number (e.g., "42" with quotes) stays a string; an unquoted 42 becomes a number. If your YAML has type-coercion bugs (e.g., a zip code 01234 becoming octal), quote it in the source YAML to force string interpretation.
Anchors and aliases
YAML anchors (&name) mark a value for reuse; aliases (*name) refer back to it. This is useful for DRY configuration:
defaults: &defaults
retries: 3
timeout: 30
production:
<<: *defaults
host: prod.example.com
staging:
<<: *defaults
host: staging.example.com
When this YAML is converted to JSON, the alias is inlined — both production and staging get their own copy of {retries: 3, timeout: 30}. JSON has no concept of shared references, so this is the only sensible behaviour. The result is larger than the YAML, but valid JSON.
Multi-document files
A single YAML file can contain multiple documents separated by ---:
---
apiVersion: v1
kind: Service
metadata:
name: frontend
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
This is common in Kubernetes for combining related resources. The converter parses each document and produces a JSON array, where each element is one document. Single-document YAML produces a single JSON object, not an array. Toggle "always wrap in array" in settings to force a consistent shape regardless of input.
Multi-line strings
YAML has two block scalar styles for multi-line strings:
- Literal (
|): Newlines are preserved exactly. Best for code, configuration blobs, and any text where line breaks matter. - Folded (
>): Line breaks become spaces, except blank lines which become single newlines. Best for prose paragraphs.
Both styles convert to JSON strings with their newlines preserved as-is. The | style keeps \n separators; the > style folds them into spaces. JSON doesn't distinguish between the two, so the output reflects what your YAML specified.
Tags and custom types
YAML allows you to explicitly tag a value's type with a !! prefix or custom ! prefix:
name: !!str 42
date: !!timestamp 2024-12-31
binary: !!binary |
R0lGODlhDAAMAIQAAP//9/X
Standard tags (!!str, !!int, !!bool, !!null, !!float, !!timestamp, !!binary) are honoured. Binary data is base64-decoded to a string. Timestamps become ISO 8601 strings (JSON has no native timestamp type). Custom tags produce a parse error — if you need them, register them in the settings panel or simplify the YAML.
Common edge cases
Most YAML-to-JSON conversions are straightforward, but a few patterns are worth knowing about:
- Norway problem: The country code "NO" is interpreted as boolean false in YAML 1.1. YAML 1.2 fixed this; modern parsers (including js-yaml 4.x) read it as a string. If you're stuck on YAML 1.1, quote "NO" in the source.
- Octal numbers:
01234in YAML 1.1 is octal (1234 decimal). In YAML 1.2, leading zeros are just integers. Quote if you need the literal string. - Trailing whitespace: Preserved in plain strings but stripped in block scalars. Quote if you need exact preservation.
- Empty values:
key:(no value) is null.key: ""is an empty string.key: ~is also null. They look the same in YAML but mean different things in JSON. - Comments and directives: Comments (
# ...) are stripped. YAML directives (%YAML 1.2) are honoured.---at the start of a file is a document start marker (not data); it's stripped during parsing. - Reserved keys at the top level: YAML 1.1's "Norway problem" and similar quirks are gone in 1.2. js-yaml 4.x is strict 1.2 — if you have older YAML, you may see errors that weren't errors before.
The same job, the way you need it
Free tools for the specific version of this conversion you are actually doing.
YAML to JSON for Kubernetes
Multi-document manifests, anchors and all - plus the yq and kubectl commands.
Frequently asked questions
What is a YAML to JSON converter?
Is YAML to JSON conversion free?
What happens to YAML anchors and aliases?
Does the converter handle multi-document YAML?
Can I convert a Kubernetes manifest YAML to JSON?
Try the YAML to JSON converter
Free, instant, and private — your YAML never leaves this tab.