YAML to JSON Converter
Turn YAML into JSON and JSON back into YAML, and tidy a YAML file while you are there. Nothing is uploaded.
Nothing you paste is uploaded. The document is read and written again in this page, and the result changes on every keystroke.
What this reader will not guess at
- An anchor (&name) marks a node so it can be repeated later. This reader keeps no such register, so it stops here instead of quietly dropping the label.
- An alias (*name) repeats a node an anchor marked earlier. This reader does not resolve aliases, so it stops here instead of inventing a value.
- An explicit tag (!something) overrides the type a value would otherwise get. This reader applies the core schema only, so it stops rather than ignore your instruction.
- A directive line (%YAML or %TAG) changes how the rest of the stream is read. This reader handles YAML 1.2 with the core schema and nothing else.
- A question mark and a space open a complex key, one that is itself a collection. Only scalar keys are read here, because a JSON object name is a string.
- A merge key (<<) folds another mapping into this one. It rests on aliases, which this reader does not resolve.
- This stream holds more than one document. JSON cannot carry a stream, so the conversion stops instead of silently keeping the first document.
- A digit in a block scalar header sets the content indentation explicitly. This reader measures the indentation from the first non-empty line instead.
- A tab is being used for indentation. YAML forbids that: indentation is spaces only.
{
"service": "checkout",
"replicas": 3,
"ports": [
8080,
8443
],
"limits": {
"cpu": "500m",
"memory": "512Mi"
},
"healthy": true,
"version": "1.0",
"notes": "first line\nsecond line\n"
}156 characters in, 220 out over 15 lines.
What a rewrite keeps
Rewriting goes through the data, not the text: the document is read into values and printed again from them. Comments, blank lines, quoting style and indentation width are presentation and do not survive the trip. The order of keys in a mapping does.
YAML and JSON carry the same three shapes — a mapping, a sequence and a scalar — so moving a document between them is mostly a question of punctuation. The interesting part is the scalars. YAML has no quotes around most values, so it has to decide from the characters alone whether "true" is a word or a boolean, whether 0o7 is a number in base eight and whether an empty value is null. That decision is written down as a table of regular expressions in the specification, and this page follows it exactly, which is why 0o14 arrives as 12 and "true" in quotes stays a string. Everything runs in this page: the file you paste is never uploaded, which matters more here than usual, because YAML is where secrets and deployment manifests live.
How it is calculated
null | Null | NULL | ~ · [-+]? [0-9]+ · 0o [0-7]+ · 0x [0-9a-fA-F]+
Those are four rows of the core schema tag resolution table. A plain scalar is matched against the whole table in order, and the specification says that "if none of the regular expressions matches, the scalar is resolved to tag:yaml.org,2002:str" — that is, treated as a string. So the rule for reading a bare YAML value is not "guess sensibly": it is a fixed list, and anything not on it is text. Going the other way, a string that would land on one of those rows comes back out in quotes, because printing it bare would change its type.
Questions people ask
- Which parts of YAML does this not handle?
- Anchors and aliases (& and *), merge keys (<<), explicit tags (!something), directives (%YAML), complex keys opened with a question mark, an explicit indentation digit in a block scalar header, and a stream holding more than one document. Each of those stops the conversion with a message naming it. That is deliberate: a reader that silently dropped an anchor would hand you a document that looks right and is not.
- Is this a complete YAML implementation?
- No, and it does not claim to be. It is a reader written for this page against the 1.2.2 specification, covering block mappings and sequences, flow collections, the three scalar styles, literal and folded blocks with their chomping indicators, comments and the core schema. The eight constructs listed above are refused by name. Outside those, an unusual document could still be read differently from how a full parser would read it, so check the result against your own file rather than trusting it blind on anything that matters.
- Why did my comments disappear?
- Rewriting goes through the data, not the text: the document is read into values and printed again from them. Comments, blank lines, quoting style and indentation width are presentation and do not survive the trip. The order of keys in a mapping does.
- Why is 0o14 now 12?
- Because the core schema resolves 0o[0-7]+ as an integer in base eight, and JSON writes integers in base ten. The value did not change; only the way it is written did. The same happens to 0xC, which is 12 as well.
- Why did quotes appear around some values on the way back to YAML?
- Because without them the value would read as something else. The string "true" printed bare would come back as a boolean, and "1.0" would come back as the number 1. Quoting is how a string that looks like another type stays a string.
- Is the mapping order kept?
- Yes. Keys come out in the order they went in, both ways. What is not kept is anything that is only presentation: comments, blank lines, the choice between a quoted and a plain scalar, and how far each level was indented.
- What happens to a literal or folded block?
- It becomes a JSON string with the line breaks the specification says it has. A literal block keeps every break; a folded block turns each one into a space unless it ends an empty or a more-indented line. Coming back the other way, a string with line breaks is written as a literal block, which is far easier to read than one long line full of escapes.
Sources
The documents this page reads its numbers out of, linked so you can check them yourself.
- YAML 1.2.2 section 10.3 — the Core Schema, "the recommended default schema that YAML processor should use unless instructed otherwise", whose tag resolution matches a plain scalar against "null | Null | NULL | ~", "true | True | TRUE | false | False | FALSE", "[-+]? [0-9]+", "0o [0-7]+", "0x [0-9a-fA-F]+" and the float shape, and resolves anything left over to tag:yaml.org,2002:str
- YAML 1.2.2 section 2.1 — "Block sequences indicate each entry with a dash and space (- )" and "Mappings use a colon and space (: ) to mark each key/value pair"
- YAML 1.2.2 section 8.1.1.2 Block Chomping Indicator — stripping by "-" excludes the final line break and any trailing empty lines, clipping is the default and preserves the final line break, keeping by "+" treats the final line break and the trailing empty lines as content
- YAML 1.2.2 section 2.3 — the folded style is "denoted by >, where each line break is folded to a space unless it ends an empty or a more-indented line", while the literal style "|" keeps every line break
Related tools
JSON Formatter and Validator
Indent, minify and check JSON as you type. When it will not parse, this page names the line and column where the text stops making sense.
Table Converter and Generator
Edit a table in a grid and take it out as CSV, Markdown, LaTeX, HTML or JSON. Everything is worked out in the page.
Base64 Encode and Decode
Turn text into Base64 and back as you type, in the standard alphabet or the URL-safe one. Nothing you paste leaves the page.
Found a problem, or want more?
A number that disagrees with its source is a defect, not a rounding preference.
What did you enter, what did the tool show, and what did you expect instead? If you have a source that disagrees with ours, a link to it is the most useful thing you can send.
Opens your mail app with the page and tool already filled in.