JSON vs YAML: Differences and What Changes When You Convert
By Durim Hoti · Last updated
JSON and YAML side by side
| JSON | YAML | |
|---|---|---|
| Structure | Braces { } and brackets [ ] | Indentation with spaces; lists with "- " |
| Strings | Always in double quotes | Quotes usually optional |
| Comments | Not allowed | Allowed, starting with # |
| Multi-line text | \n inside one string | Block styles | and > |
| Several documents in one file | No | Yes, separated by --- |
| Typical use | APIs, data exchange, package.json | Config files: CI pipelines, Docker Compose, Kubernetes |
Convert between JSON and YAML step by step
- Open YAML to JSON or JSON to YAML.
- Paste the data or click Open file. The result appears on the right.
- Choose the indentation (2 or 4 spaces; tabs are also possible for JSON).
- Compare the values on both sides, especially version numbers, codes with leading zeros and words like yes, no, on and off.
- Click Copy or Download.
Example: values that change from YAML to JSON
A small service config, as people often write it:
# Service config
name: shop-api
version: 1.10
country: no
zip: 01234
time: 12:30
ports:
- 8080
- 8443
{
"name": "shop-api",
"version": 1.1,
"country": "no",
"zip": 1234,
"time": "12:30",
"ports": [
8080,
8443
]
}
- 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 readnoasfalseand12:30as 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:
{"name":"shop-api","version":"1.10","country":"no","zip":"01234","ports":[8080,8443],"debug":null,"on":true}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
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
- Open the JSON to YAML converterConvert JSON to readable YAML.
- How to Fix JSON Errors Like "Unexpected token"Understand JSON error messages, find the exact spot, and fix the nine most common mistakes.
- How to Format HTML Code So It's ReadableIndent minified or messy HTML, choose how attributes wrap, and spot broken markup.