Home JSON to YAML
JSON to YAML converter
Paste JSON and get block-style YAML back — for a Kubernetes manifest, a CI workflow, an OpenAPI file or a config. It runs in this tab, and it quotes every string a YAML parser would otherwise read as something else.
Other languages:TürkçeDeutsch中文हिन्दीРусский
The strings YAML would quietly retype
In JSON a string is always in quotes. In YAML most strings are not, and an unquoted value is
typed by what it looks like. That is how a list of country codes ends up with Norway as
false — the “Norway problem” — and it is the bug a careless JSON-to-YAML
conversion introduces. The output here quotes any string that some YAML reader would load as
something other than that string:
| JSON string | Unquoted, a YAML 1.1 reader (PyYAML) loads | Written here as |
|---|---|---|
| "NO" | false | "NO" |
| "yes", "on", "off" | true, true, false | "yes", "on", "off" |
| "010" | 8 (octal) | "010" |
| "12:30" | 750 (base 60) | "12:30" |
| "2024-01-01" | a date object | "2024-01-01" |
| "1_000" | 1000 | "1_000" |
| "null", "~", "" | null | "null", "~", "" |
YAML 1.2 dropped most of these rules, but a file is read by whatever its consumer runs, and
PyYAML, Ruby's Psych and many CI tools still read YAML 1.1. Quoting for both costs a few
characters and removes the question. The same applies to keys — including y and
n, which the YAML 1.1 specification lists as booleans, so a y
coordinate is written as "y". Strings that start with an indicator character
(- # & * ! | > % @), contain ": " or " #", or have
leading or trailing spaces are quoted for the same reason: unquoted, they would not parse
back as the same text.
What the YAML looks like
- Block style throughout, two-space indentation, sequence items indented under their key — the shape
kubectland most style guides use. - Multi-line strings become literal blocks:
|when the string ends in one newline,|-when it ends in none and|+when it ends in several, so the exact text — trailing newlines included — reads back unchanged. - Empty collections stay inline as
[]and{}. - Numbers are written as they were —
12345678901234567890keeps every digit and1.50keeps its zero — because the JSON is read by an exact parser, notJSON.parse. - Key order is kept; a duplicate key resolves the way
JSON.parseresolves it, the last value at the first key's position.
Every conversion is checked by reading the YAML back: the test suite converts JSON to YAML and YAML to JSON and requires the same document. Before release the writer was also run on 3,000 generated documents, each read back by an independent parser (js-yaml) without a single difference.
What it does not do
It does not invent structure JSON does not have: no comments (JSON has none to carry over),
no anchors to deduplicate repeated objects, no flow style. It does not add a
--- document marker or a %YAML directive. And it does not validate
against a schema — whether the result is a valid Kubernetes object is a question for
kubectl --dry-run, not for a format converter.
Common questions
Why are some of my strings quoted and others not?
A string is quoted only when a YAML reader would load it as something else unquoted — NO as false, 007 as a number, 12:30 as 750, 2024-01-01 as a date — or when it starts with a YAML indicator character, contains ": " or " #", or has leading or trailing spaces. Everything else is written plainly.
What is the Norway problem?
YAML 1.1 treats NO, no, yes, on and off as booleans, so the country code for Norway, unquoted, loads as false. This converter quotes such strings so the value survives any YAML parser.
How are multi-line strings written?
As literal block scalars. The chomping indicator (|, |- or |+) is chosen from the number of trailing newlines, so the text reads back exactly, including how it ends.
Does it keep large numbers exactly?
Yes. The JSON is parsed with an exact reader rather than JSON.parse, so integers above 2^53 keep every digit and 1.50 keeps its trailing zero in the YAML.
Is the output YAML 1.1 or 1.2?
It is valid for both, and quoted so that a YAML 1.1 reader such as PyYAML and a YAML 1.2 reader load the same data.
Related
- /yaml-to-json-converter — the other direction, with anchors, merge keys and multiple documents.
- /json-formatter — validating the JSON first, with errors that name the line.
- /json-to-json-schema — a JSON Schema for the same document.
- /api-mock-data — generating the JSON in the first place.
Last updated