What the YAML to JSON converter does
The converter reads YAML by the core schema of YAML 1.2 (opens in a new tab) and writes the same data as JSON, so you can hand a configuration file to an API, a JSON tool or your code, or simply see what a parser makes of it. JSON has no comments, anchors, tags or multiple documents per value, so the converter expands, reshapes or drops those, and the Problems list says where. Numbers are copied digit for digit, and YAML's own forms, like 0x1F, are converted to exact decimal values.
How to convert YAML to JSON
- Paste YAML, or open or drop a
.yamlor.ymlfile. - Read the Problems list: a warning marks a value that other YAML tools may read differently, with its line and column.
- Choose the Output layout, and how to write a file of Multiple documents.
- Copy the JSON or download it: a
.jsonfile, or.jsonlfor JSON Lines. Swap direction sends the result to JSON to YAML.
Examples
Input
base: &base
timeout: 30
retries: 3
fast:
<<: *base
timeout: 5
Output
{
"base": {
"timeout": 30,
"retries": 3
},
"fast": {
"timeout": 5,
"retries": 3
}
}
Input
answer: no
mode: 0644
window: 1:30
Output
{
"answer": "no",
"mode": 644,
"window": "1:30"
}
Input
Bucket: !Ref LogsBucket
Mask: 0xFF
Output
{
"Bucket": "LogsBucket",
"Mask": 255
}
Anchors, aliases and merge keys
An anchor (&name) labels a value, and an alias (*name) repeats it elsewhere in the same document. JSON has no references, so every alias is replaced by a full copy of the anchored value. An alias takes the last value with its name before it; one that comes before its anchor, or sits in another document, is an error.
A merge key, <<, copies the keys of a mapping into the mapping it appears in, which is how Docker Compose files share settings between services. The merged keys take the place of the << line, and three rules decide which value wins:
- a key written in the mapping itself beats a merged one, wherever it stands;
- with a list, as in
<<: [*defaults, *logging], an earlier mapping beats a later one; - a merged mapping brings its own merges along.
Merge keys come from YAML 1.1 and are not part of YAML 1.2, but most parsers still support them. Turn off Resolve merge keys to see << as an ordinary key holding the other mapping.
Copies multiply when aliases point to values that contain more aliases: nine levels of nine aliases each, the "billion laughs" pattern, would make a document of billions of values from a few lines of YAML. The converter therefore stops after 10,000 aliases or 1,000,000 values written through them, far more than any real configuration file uses.
Several documents in one file
A line of three dashes, ---, starts a new document, which is how Kubernetes keeps several manifests in one file. JSON holds one value, so documents become the items of a JSON array by default. Choose JSON Lines to get one compact document per line instead, the format that jq -c and many log tools read. A document with no content, such as a --- at the very end of the file, becomes null. A file with a single document is written as that document's value, whatever this option says.
YAML 1.1 and the Norway problem
YAML 1.1, published in 2005, read many plain words and numbers differently from YAML 1.2. In 1.1, the country code NO is the boolean false, which is why this pitfall is called the Norway problem. The converter follows YAML 1.2 and warns about every unquoted value that YAML 1.1 reads otherwise:
- the YAML 1.1 booleans (opens in a new tab)
y,n,yes,no,onandoff, in lowercase, capitalized or uppercase; - numbers with a leading zero, which are octal in 1.1:
0644is 644 in YAML 1.2 but 420 in YAML 1.1; - times like
1:30, which 1.1 reads as a base-60 number, 90; - digits grouped with underscores, like
1_000, and binary numbers like0b101; - dates like
2024-01-01, which 1.1 reads as a timestamp; - YAML 1.2 number forms that 1.1 reads as text, such as
1e3and0o755.
The warning matters when another program reads the same file. PyYAML, which Ansible uses, still follows YAML 1.1, like many parsers written before YAML 1.2 appeared in 2009. Quoting a value makes it a string in every version: "NO" is safe everywhere.
Custom tags like !Ref
A tag, written with an exclamation mark, tells a program how to build a value. AWS CloudFormation uses !Ref, !Sub and !GetAtt; YAML 1.1 defined !!binary, !!set and !!omap. JSON has no tags, so the converter writes each value with its own structure, a scalar as a string and a mapping as an object, and warns at the tag. The tag's meaning is lost: !Ref LogsBucket becomes the plain string "LogsBucket", which CloudFormation would not resolve. The core tags !!str, !!int, !!float, !!bool, !!null, !!map and !!seq work as they say, so !!str 123 gives the string "123".
Comments are left out
JSON has no syntax for comments, so the converter drops every # comment and reports how many there were. Keep the YAML as the file you edit when its comments matter. If a note must travel with the data, move it into a value, such as a description key that the program reading the JSON ignores.
Hex, octal, .inf and .nan
YAML 1.2 has number forms that JSON does not, and the converter turns them into JSON numbers of exactly the same value: 0x1F becomes 31 and 0o755 becomes 493, computed with arbitrary precision so a long hex value keeps every digit. +1 becomes 1, .5 becomes 0.5 and 1. becomes 1.0. Numbers that JSON already allows, like 1e3 or 12345678901234567890, are copied as written. A whole number above 9,007,199,254,740,991 gets a note, as most JSON readers round it; the JSON Formatter explains why.
.inf, -.inf and .nan have no JSON form at all. By default they stop the conversion; set .inf / .nan values to String to keep them as text, or to null.
Options
| Option | Values | Default | When to change it |
|---|---|---|---|
| Output | Pretty, 2 spaces, Pretty, 4 spaces, Minified | Pretty, 2 spaces | Minified JSON is smallest, for an API request or a fixture; pretty JSON is for reading. |
| Multiple documents | JSON array, JSON Lines | JSON array | JSON Lines writes one document per line, for log pipelines and jq -c; it applies only to files with several documents. |
| Resolve merge keys (<<) | On, Off | On | Turn it off to see << as an ordinary key, for example to check what a file merges. |
| .inf / .nan values | Error, String, null | Error | String keeps the YAML text, like ".inf"; null is what JavaScript's JSON.stringify writes for these values. |
Common errors and fixes
| Code | What it means | How to fix it |
|---|---|---|
YAML_Error | The text is not valid YAML at this place: a missing colon, an unclosed quote or bracket, or uneven indentation. | Follow the message and the hint; an indentation error is often on the line above the one marked. |
YAML_Error | A line is indented with a tab character. YAML allows only spaces for indentation. | Replace the tab with spaces, or set your editor to insert spaces for .yaml files. |
YAML_Error | A mapping has the same key twice, and a JSON object holds one value per key. A number key and its quoted form, like 1 and "1", also clash. | Remove or rename one of the keys; the related position shows the first one. |
YAML_Error | An alias names an anchor that is not defined before it in the same document. | Define the anchor first, check the spelling, or copy the value into this document. |
YAML_Error | An alias sits inside the very value it refers to, so copying it would never end. | Point the alias at a value outside its own anchor. |
YAML_Error | Expanding the aliases would make more than 10,000 copies or 1,000,000 values, as in a billion laughs attack. | Use fewer nested aliases; a real configuration file stays far below the limit. |
YAML_Error | The value of a << merge key is not a mapping, an alias to one, or a list of them. | Point << at an anchored mapping, like <<: *defaults, or turn off Resolve merge keys. |
YAML_Error | The YAML has .inf, -.inf or .nan, which JSON numbers cannot express. | Set .inf / .nan values to String or null, or replace the value in the YAML. |
YAML_Error | A key is itself a mapping or a list, written after ?, and JSON keys can only be strings. | Use a string key and move the structure into the value. |
YAML_Warning | A key is a number, a boolean or null in YAML, like 200: or true:, and becomes a string in JSON. | Quote the key in the YAML if a string is what you meant; the JSON is the same either way. |
YAML_Warning | A tag like !Ref has no JSON form, or a core tag like !!int does not fit its value. | Expect the plain value in the JSON; a program that needs the tag has to read the YAML itself. |
YAML_Warning | An unquoted value means something else to YAML 1.1 parsers, like NO, 0644 or 1:30. | Quote the value to keep a string everywhere, or write the number in a form both versions read alike. |
YAML_Error | Mappings and lists are nested more than 512 levels deep. | Flatten the structure; no configuration needs this many levels. |