JSON to YAML: the complete guide
The YAML format, the conversion rules, and how YAML behaves in real infrastructure.
YAML syntax basics
YAML uses three structural markers: indentation for hierarchy, colons for key-value pairs, and dashes for list items. Whitespace is significant — YAML does not use braces or brackets. A simple document:
name: web-server
replicas: 3
image: nginx:1.25
ports:
- 80
- 443
labels:
app: web
tier: frontend
The indentation (2 spaces per level) creates the hierarchy. A dash followed by a space introduces a list item. A colon followed by a space separates a key from its value. That's the whole syntax — YAML 1.2 is designed to be the most readable structured data format ever standardised.
YAML is also a strict superset of JSON: any valid JSON document is also a valid YAML document. That's what makes the conversion lossless.
How does the conversion work?
JSON has six value types: object, array, string, number, boolean, null. YAML has the same six types, plus extras like timestamps and binary data. The conversion is one-to-one for the shared types, with these rules:
- Objects become block-style mappings with each key on its own line, indented under the parent.
- Arrays become block-style sequences with each item prefixed by a dash.
- Strings stay unquoted unless they contain special characters or look like other types (numbers, booleans, null).
- Numbers stay as plain digits (no quotes), matching YAML's native number type.
- Booleans are emitted as
trueorfalse(lowercase, the YAML 1.2 canonical form). - Null is emitted as
nullor~(both are valid YAML).
JSON keys are always strings, so they become unquoted YAML keys (the standard practice). If a key would be ambiguous (e.g., contains a colon or starts with a number), the converter adds quotes automatically.
When does YAML need quotes?
YAML is permissive about quoting but strict about ambiguity. A string that could be parsed as a number, boolean, or null must be quoted to be treated as a string. Examples:
version: 1.0— parsed as a number unless quoted ("1.0")active: true— parsed as a boolean unless quoted ("true")empty: null— parsed as null unless quotedon: yes— parsed as boolean true (YAML 1.1 compatibility)code: 01234— parsed as octal in some parsers; quote to be safe
Strings that contain special characters (colons, hashes, leading/trailing whitespace, newlines, leading dashes) also need quoting. The converter applies quoting automatically using these rules. Toggle "Quote all strings" in settings if you want every string to be quoted explicitly — useful for diff stability and for users unfamiliar with YAML's ambiguity rules.
Multi-line strings
JSON has no real multi-line string type — embedded newlines must be escaped as \n. YAML supports two block scalar styles:
- Literal (
|): Newlines are preserved exactly. Best for code, configuration blobs, and any text where formatting matters. - Folded (
>): Newlines become spaces, except for blank lines which become single newlines. Best for prose where you'd otherwise use one long line.
When converting JSON, the converter can't know which style you want (the JSON doesn't encode the difference), so it defaults to literal (|) for any string containing a newline. You can change the default in settings or by post-processing the file.
Anchors and aliases
YAML has a feature JSON lacks: anchors (&name) and aliases (*name). An anchor marks a value for reuse; an alias refers back to it. This is useful for DRY configurations:
defaults: &defaults
retries: 3
timeout: 30
production:
<<: *defaults
host: prod.example.com
staging:
<<: *defaults
host: staging.example.com
JSON has no equivalent, so converting a YAML file with anchors to JSON will inline the values (losing the DRY structure). Converting JSON to YAML can't create anchors automatically — the input has no concept of "this is the same as that." If you need anchors, add them by hand after the conversion.
Kubernetes conventions
Kubernetes uses YAML 1.2 with a few conventions worth knowing:
- 2-space indent. Always. Tools like
kubectl applyandhelm installdon't care, but humans reading the file do. - Lowercase keys.
apiVersion,kind,metadata,spec. The schema is case-sensitive. - apiVersion first, then kind, then metadata, then spec, then status. Most YAML sorters will reorder keys to this convention; if yours doesn't, do it by hand.
- Strings with template syntax (
{{ .Values.foo }}) should be quoted to avoid YAML's type-coercion rules interpreting.Values.fooas a number. - Multi-document YAML (multiple
----separated documents in one file) is common for k8s manifests. The converter doesn't split or merge these — one JSON document becomes one YAML document.
Common edge cases
Most JSON-to-YAML conversions are straightforward, but a few patterns are worth knowing about:
- Empty objects and arrays: Emitted as
{}and[](flow style). This is the most portable representation across parsers. - Empty strings: Emitted as
""(quoted) to distinguish them from null and to make the column width obvious in editors. - Unicode: Preserved as-is. YAML is UTF-8 by default, so emoji and non-Latin characters round-trip cleanly.
- Very long strings: The converter doesn't wrap them. If you need a hard wrap, post-process the output (or use YAML's folded scalar style).
- Numbers that look like dates:
20241231stays a number."2024-12-31"stays a string. YAML 1.2 doesn't auto-parse dates (that's a YAML 1.1 quirk). - Trailing whitespace in strings: Preserved. YAML parsers handle this correctly; some text editors may strip it on save.
Frequently asked questions
What is a JSON to YAML converter?
Is JSON to YAML conversion free?
What is YAML used for?
Is YAML 1.2 supported?
Can I convert a Kubernetes manifest JSON to YAML?
Try the JSON to YAML converter
Free, instant, and private — your JSON never leaves this tab.