Skip to content
Technology·1 min read·

YAML and JSON converter

Convert YAML to JSON, or JSON to YAML, without sending the text to another service.

The answer

YAML is parsed with a YAML 1.2 reader and printed as indented JSON. JSON is parsed and printed as YAML. Aliases are limited so a huge alias expansion cannot stall the page.

Result

JSON

{
  "ok": true
}

What do you want to do next?

How this works

YAML to JSON reads one document. JSON to YAML requires legal JSON, then prints a YAML document. Comments in YAML are not kept, because JSON has nowhere to put them.

Custom tags and enormous alias graphs are out of scope. If the text uses anchors heavily, simplify it before converting. The result is data, not a promise that a particular application will accept the keys.

Practical steps

  1. 1

    Choose the direction that matches what you pasted.

  2. 2

    Paste one document.

  3. 3

    If YAML fails, look for a tab in the indentation or a tab character used as a space.

Examples

  • ok: true

    The JSON is { "ok": true }.

Common mistakes

  • ×

    Expecting the YAML indentation to survive a round trip through JSON unchanged.

Frequently asked questions

Are comments preserved?

No. The conversion keeps the data, not the comments.

Can I convert several YAML documents at once?

No. Paste one document.

Continue reading

Sources