Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Your JSON is already valid YAML. The converter just makes it readable

Your JSON is already valid YAML. The converter just makes it readable Every valid JSON document is already valid YAML. That is not a trick sentence. YAML 1.2 was designed as a superset of JSON, which means this: {

Your JSON is already valid YAML. The converter just makes it readable

Every valid JSON document is already valid YAML. That is not a trick sentence. YAML 1.2 was designed as a superset of JSON, which means this:

{
  "name": "api-server",
  "replicas": 3,
  "debug": false
}

is legal YAML, right now, with zero changes. You could paste it into a .yaml file and a YAML parser would read it correctly.

So why does anyone need a JSON to YAML converter?

Because "valid" and "readable" are different things. Nobody writes a Kubernetes manifest in JSON braces. Nobody reviews a GitHub Actions workflow full of quotes and commas. The conversion is not about changing the data. It is about changing the shape so a human can read it, edit it, and review it without going cross-eyed.

What actually changes in the conversion

The data stays identical. What changes is the syntax:

name: api-server
replicas: 3
debug: false

Braces become indentation. Quotes disappear where they are not needed. The structure becomes visible at a glance instead of hidden behind punctuation.

That is the whole job, and it is also where converters earn their keep, because the details are where a naive converter silently breaks your config.

The quoting trap: "yes" is not always a string

Take this JSON:

{ "answer": "yes", "version": "1.10", "code": "0123" }

A careless converter writes:

answer: yes
version: 1.10
code: 0123

And now three values have changed meaning. yes parses as boolean true in YAML 1.1 parsers (and plenty of tools still use 1.1 rules). 1.10 is now a float, not the string "1.10". 0123 is an octal number in some parsers, not the string "0123".

A correct converter quotes all three:

answer: "yes"
version: "1.10"
code: "0123"

The rule is simple to state and easy to get wrong: quote a string whenever YAML would misread it as something else. That covers booleans (yes, no, on, off, true, false, null), numbers (123, 1.5, 0x1F), and anything with a colon, hash, or leading space. A good converter does this automatically. A bad one makes you discover it in production.

Indentation is a convention, not a law

YAML accepts any consistent indentation, but the ecosystem has settled on two spaces. Kubernetes manifests, Docker Compose files, GitHub Actions workflows: all two spaces. Four spaces works, but it pushes nested config off the right edge of the screen fast, and it looks wrong to every reviewer who has ever read a manifest.

If your converter offers an indent choice, pick 2 spaces for anything that will live in a repo. Save 4 spaces for the rare case where you are generating YAML for a tool that demands it.

Why this runs in your browser

Config files contain internal hostnames, API keys, service names, and infrastructure details. That is exactly the kind of text you should not paste into a random server.

A JSON to YAML conversion is pure text transformation. There is no reason for it to leave your machine, and a converter that runs entirely in your tab is the honest way to build one. Paste the JSON, get YAML, nothing uploaded.

I built one that does exactly that, with the quoting rules above handled for you: JSON to YAML Converter. Paste JSON or drop a .json file, pick 2 or 4 space indent, sort keys if you want them alphabetical, and copy or download the .yaml. It also flags invalid JSON with the parser's own error message instead of silently producing garbage.

The reverse direction matters too

Converting YAML back to JSON is the other half of the same workflow: your API wants JSON, your manifest is YAML. The same quoting rules apply in reverse, plus anchors and merge keys that JSON has no equivalent for. If you need that direction, the YAML to JSON converter on the same site handles it, anchors included.

The one-line takeaway

JSON and YAML describe the same data. JSON is the machine-readable shape, YAML is the human-readable shape, and the converter between them is mostly a quoting engine with good taste. Pick one that quotes "yes", and you will never debug a config that changed meaning in transit.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.