Skip to content

JSON vs YAML: Differences and What Changes When You Convert

By Durim Hoti · Last updated

JSON and YAML side by side

 JSONYAML
StructureBraces { } and brackets [ ]Indentation with spaces; lists with "- "
StringsAlways in double quotesQuotes usually optional
CommentsNot allowedAllowed, starting with #
Multi-line text\n inside one stringBlock styles | and >
Several documents in one fileNoYes, separated by ---
Typical useAPIs, data exchange, package.jsonConfig files: CI pipelines, Docker Compose, Kubernetes

Convert between JSON and YAML step by step

  1. Open YAML to JSON or JSON to YAML.
  2. Paste the data or click Open file. The result appears on the right.
  3. Choose the indentation (2 or 4 spaces; tabs are also possible for JSON).
  4. Compare the values on both sides, especially version numbers, codes with leading zeros and words like yes, no, on and off.
  5. Click Copy or Download.

Example: values that change from YAML to JSON

A small service config, as people often write it:

service.yaml
# Service config
name: shop-api
version: 1.10
country: no
zip: 01234
time: 12:30
ports:
  - 8080
  - 8443
JSON
{
  "name": "shop-api",
  "version": 1.1,
  "country": "no",
  "zip": 1234,
  "time": "12:30",
  "ports": [
    8080,
    8443
  ]
}
The YAML to JSON converter. The YAML input on the left has a comment, name: shop-api, version: 1.10, country: no, zip: 01234, time: 12:30 and two ports. The JSON output on the right shows version as the number 1.1, country as the string no, zip as the number 1234 and time as the string 12:30; the comment is gone.
The comment is gone, 1.10 became 1.1 and 01234 became 1234.
  • version: 1.10 is a number in YAML, so it became 1.1. For a version, that is wrong: 1.10 and 1.1 are different releases.
  • zip: 01234 was read as the integer 1234; the leading zero is lost.
  • country: no stayed the string "no", and time: 12:30 stayed "12:30". Our converter follows YAML 1.2. Older YAML 1.1 parsers read no as false and 12:30 as the base-60 number 750 (12 × 60 + 30), so the same file can give different data in different tools.
  • The comment was dropped, because JSON can't hold comments.

The fix is the same for all of them: put quotes around values that must stay text, for example version: "1.10" and zip: "01234".

From JSON to YAML: why some values get quotes

Going the other way, the converter protects such values for you:

JSON
{"name":"shop-api","version":"1.10","country":"no","zip":"01234","ports":[8080,8443],"debug":null,"on":true}
YAML
name: shop-api
version: "1.10"
country: "no"
zip: "01234"
ports:
  - 8080
  - 8443
debug: null
"on": true

"1.10", "no" and "01234" got quotes, and so did the key "on", because a YAML 1.1 parser would read on as true. Plain strings like shop-api stay unquoted.

Common problems

Tabs in YAML

YAML indentation must use spaces. A tab gives this error in our validator: Tabs are not allowed as indentation. YAML doesn't allow tabs for indentation — use spaces. Set your editor to insert spaces for YAML files.

A key appears twice

YAML
name: shop-api
name: shop-web

Some parsers silently keep the last value. Our validator rejects it: Map keys must be unique. Each key can appear only once in the same mapping.

The JSON is an array instead of an object

The YAML file contains several documents separated by ---. The converter then returns an array with one entry per document and says so in a note.

The JSON won't convert at all

The JSON itself is invalid. Our guide on fixing JSON errors explains the messages and the automatic repair.

What to do next

The YAML 1.2 rules are in the YAML specification. To tidy up YAML without converting it, use the YAML formatter. Working with HTML instead? See how to format HTML code.

FAQ

Is JSON valid YAML?
Practically yes: YAML 1.2 was designed so that JSON documents are valid YAML. That is why you can paste JSON into most YAML files. The reverse is not true; YAML comments, anchors and unquoted strings aren't JSON.
Which one should I use for a config file?
Use whatever the tool expects. If you can choose, YAML is easier to read and allows comments; JSON is stricter, so typos are caught earlier and every language can parse it without an extra library.
Are comments kept when converting YAML to JSON?
No. JSON has no comments, so they are dropped. Keep the YAML file as your source if the comments matter.
Why does the JSON to YAML converter put quotes around some values?
Values such as "no", "on", "1.10" or "01234" would be read as booleans or numbers by YAML 1.1 parsers like PyYAML. Quoting them keeps them strings in every parser.
Is my data uploaded?
No. Both converters and the YAML validator run in your browser.

Try it and keep reading