StringMash.com

JSON to YAML

Write JSON out as YAML, with quotes where they matter and nowhere else.

Conversion
88 characters
Updates as you type
Two-space indentation

Using the converter

Paste JSON on the left and YAML appears on the right, indented two spaces per level. Swap the boxes to read YAML back.

An empty object or array is written in flow style as {} or [], because written in block form an empty collection would be a blank line.

Quoting is the whole job

Writing YAML is easy until you reach a string that would read back as something else. The value 12 as a string has to be written "12" or it returns as a number. So do true, null and ~, and yes is a boolean in YAML 1.1.

Strings starting with a marker need quotes too. A leading dash reads as a sequence entry, a leading hash starts a comment, and a colon followed by a space anywhere in the value splits it into a key and a value. Leading or trailing spaces are invisible in the file and would be lost.

This converter tests each string by reading it back. If a bare value would resolve to anything other than that exact string, it gets quotes. It also checks the older YAML 1.1 rules, which PyYAML and Ruby still use. So yes, NO, off, 1_000, 1:30 and a date like 2020-01-01 are quoted as well. Everything else stays bare, so the output reads like YAML a person wrote.

JSON is already YAML

Since YAML 1.2, published in 2009, JSON is a strict subset of YAML. Every valid JSON file is a valid YAML file, and a conforming YAML parser will read it without complaint.

So this converter only reformats. The data doesn't change at all. The braces and brackets become indentation, the quotes come off the keys, and the commas go.

The relationship runs one way only. YAML has comments, anchors and several scalar styles, none of which JSON can hold, so the return trip loses whatever the file was using.

Related conversions

YAML to JSON is this converter reversed, and it explains what the reader will and won't accept. JSON to XML is the same idea for a format that needs a single root element.

For flattening a list of objects into rows instead, use JSON to CSV.

Questions

How do I convert JSON to YAML?

Paste the JSON above. The result uses two-space indentation and quotes only the strings that would otherwise read back as something else.

Is JSON valid YAML?

Yes, since YAML 1.2 in 2009. JSON is a strict subset, so any conforming YAML parser will read a JSON file unchanged.

Why are some of my strings in quotes?

Because without them they'd come back as a different type. "12" would be a number, "true" a boolean and "null" nothing at all. Words like yes and off are quoted too, because older YAML readers take them as booleans.

Will comments survive a round trip?

No. JSON has no way to hold a comment, so converting YAML to JSON and back loses every one of them, along with anchors and the choice of quoting style.

Sources

Added . Last updated and reviewed . What's new