About JSON to YAML
Writing YAML from JSON looks trivial until a string changes meaning. A version of "1.10" becomes the number 1.1 if written bare, the country code "no" becomes false in many parsers, and an empty string turns into null. This converter quotes those cases and leaves everything else clean.
It is handy when configuration begins life as JSON, for instance from an API or a generated file, and has to end up in a Helm values file, a Compose file or a CI pipeline, where people will read and edit it.
You get YAML with two or four space indentation. Long multi-line strings come out as readable blocks, not one line full of \n escapes.
How it works
Each string is checked against the patterns a YAML reader could interpret: booleans in both the 1.1 and 1.2 sense, null, numbers in all their forms, dates, and characters such as :, # and [ in positions where they mean something. A match gets double quotes, using JSON escaping, which every YAML parser understands. All other strings are written plain.
A string with line breaks becomes a | block, or |- when it does not end in a newline, so the exact text comes back when read. Strings where a block could not preserve the text, such as those with leading spaces or control characters, stay in double quotes.
We tested this by generating thousands of random documents full of awkward strings, writing each one to YAML and parsing it with two separate parsers. Every one came back identical.
How to use JSON to YAML
- Paste your JSON, or use Open file to load one.
- A syntax problem shows its line and column on the right, so fix that before going further.
- Pick 2 or 4 spaces under Options, and turn on Quote every string if your linter wants it.
- Save the result via Copy or Download .yaml.
Strings that would change meaning if left bare
{"version":"1.10","replicas":3,"country":"no","startup":"migrate\nserve","ports":[8080,8443],"empty":{}}version: "1.10"
replicas: 3
country: "no"
startup: |-
migrate
serve
ports:
- 8080
- 8443
empty: {}version and country are quoted because bare they would read as a number and a boolean. startup became a block, and the number replicas needs no quotes.
When it helps
- Starting a Helm values file from JSON returned by an API.
- Turning a generated OpenAPI document into a form people can review in a pull request.
- Moving a tool's JSON settings to a YAML format it also accepts.
- Writing a Compose or CI snippet from data you already have in JSON.
Tips for a better result
- Check the output in the system that will use it. Helm and similar tools run templates over YAML, and
{{ }}in a value can mean something to them even though it is valid YAML here. - Key order is preserved from your input, except that JSON parsers move integer-like keys to the front.
- Use Quote every string only if something requires it. It makes the file noisier, and it is not needed for accuracy.
Good to know
- Anchors and aliases are never produced, so repeated data is written out in full each time.
- Comments cannot be added, because JSON holds none to carry across.
- Numbers keep their value, not their spelling:
1.50in JSON is written as1.5. - An integer too long to store exactly is written as a quoted string so that no digits change, and a notice says so.
Questions people ask
Why are some of my strings quoted and others not?
Quotes appear only where a bare value could be read as something other than a string. name: Ada is safe, while country: "no" needs them, because older parsers would read no as false.
Will the output parse back to exactly the same data?
Yes, apart from number spelling and key order for integer-like keys. It round-trips through this site's parser and through an independent YAML library in our test runs.
What happens to null and empty values?
Null is written as null, an empty string as "", an empty list as [] and an empty object as {}. Each stays distinct when read back.
Is JSON already valid YAML?
Nearly always, since YAML 1.2 was designed to be a superset of JSON. The reason to convert is readability and the ability to add comments later.
What to use next
- YAML to JSON: to confirm how the YAML reads back
- YAML Validator: to check the file after editing it by hand
- JSON Validator: if the source JSON will not parse
Learn more
Last reviewed . Read our editorial policy.