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 the block form of an empty collection is a blank line and a blank line says nothing.

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 yes.

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. 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.

That makes this converter a reformatter rather than a translator. The data does not change at all. What changes is the punctuation: 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, several scalar styles and a date type, 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 will not 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 would come back as a different type. "12" would be a number, "true" a boolean and "null" nothing at all.

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.