ToolsTray

Star a tool to keep it here.

YAML to JSON Converter (and back)

Convert YAML to JSON, or JSON back to YAML. Output comes back pretty-printed at two-space indent, and a manifest that won't parse gets an error naming the line.

JSON

The converted document lands here.

Two-space indentation, and a line naming every scalar the parser retyped.

Direction

What YAML decided

—scalars came back retyped

How to use

  1. Put your config in the left well. A docker-compose.yml, a stream of Kubernetes resources, a workflow file: anything YAML.
  2. Check the Direction under the wells. YAML → JSON is chosen when the page loads, so JSON input wants the other chip.
  3. Read the converted text on the right, and the count of retyped scalars under it before you trust a version number or a file mode.
  4. Press Copy output when you just need to paste it into a terminal or a ticket, or Download to save it: YAML → JSON writes output.json, and the reverse writes output.yaml.

About this tool

Every config file I write by hand is YAML, and half the things that read it want JSON. So converting YAML to JSON here is one paste: drop the text into the YAML well, leave the Direction where it starts, and the JSON builds itself beside it as you type. The swap between the wells sends the result back as the input and flips the direction, which is the quickest way to check a round trip.

The JSON that comes back is indented two spaces per level. Nested services and port arrays stay stacked and readable instead of collapsing onto one line. Going the other way, JSON → YAML emits block style, with list items indented under their key rather than crammed into square brackets, which is the shape you would actually commit to a repo.

Parsing runs through js-yaml on the YAML 1.2 core schema with merge keys switched on, so <<: *defaults merges the way docker-compose files expect while NO, yes and on stay plain strings. A stream split by --- lines comes back as a JSON array, one entry per document. A custom tag meant to construct some other type gets refused rather than honoured, which is worth knowing when the manifest arrived from somewhere you don't control.

Mostly I reach for this to check my own work. A compose file that is behaving oddly reads very differently as JSON, where the nesting is explicit and a key you assumed was a list turns out to be one long string. Under the panes the page counts every scalar that came back as a different type than it was written, 1.10 that became 1.1 or 0755 that became 755, with a card for each. When the parse fails, the last good output stays, dimmed, and the line js-yaml tripped on is named; with YAML the cause is a tab or a half-indent nine times out of ten.

A four-line file, out and back

Start with name: app on one line, then ports: with - 80 and - 443 indented under it. Convert on YAML → JSON and it comes back as seven lines, with the two port numbers sitting on their own rows at four spaces of indent. They arrive as real numbers, 80 and 443, not as quoted strings.

Now switch the toggle to JSON → YAML and paste that JSON back. What returns is character for character the file you began with. It only round-trips that cleanly because there was nothing in it but data. Put one # comment in and the return trip drops it.

Good to know

  • Comments are gone after any conversion. They are not data, so a compose file you annotated line by line comes back bare.
  • Anchors and aliases expand. The repeated block is correct, but the &defaults and *defaults link that generated it does not exist in the output.
  • Formatting choices get normalised. Quote style, blank lines, and a folded > block scalar all come out written the way JSON.stringify or js-yaml's dump prefers, not the way you had them.

Frequently asked questions

Why does a file of comments convert to the word null?

A YAML document that holds only comments parses to nothing at all, and null is JSON's spelling for nothing, so that is what comes back. A box that is blank or only whitespace converts to nothing yet, and both wells wait for something to read.

Can I paste JSON into the YAML side?

You can. YAML is a superset of JSON, so the parser reads a JSON document without complaint and hands the same data back pretty-printed. Treat that as a reformat. For the indented YAML form you want the JSON → YAML direction.

Does yaml to json conversion keep my key order?

Map keys stay in the order they appeared, both directions. js-yaml preserves insertion order and JSON.stringify writes keys out in that order, so diffing the converted file against the original is still readable. Sequence items keep their order too.

How do I convert a Kubernetes file with several resources in it?

Paste the whole file. Each resource between --- separators becomes one entry of a JSON array, in the order they appear, and the strip under the panes says how many documents it read. A single document keeps its own shape, an object rather than an array of one.

Could a YAML file I didn't write do something nasty when I parse it?

No. The default schema only produces strings, numbers, booleans, nulls, timestamps, maps, and sequences. Tags that ask for anything beyond that are rejected with a parse error, and nothing in the file is executed as code.

Missing something in YAML to JSON Converter (and back)? Suggest a feature →

Tell someone who needs this.