Skip to content

YAML to JSON Converter

Paste or open a YAML file to see it as JSON: anchors and merge keys are expanded, several documents become an array, and every value that YAML 1.1 tools read differently, like NO or 0755, is flagged. Syntax errors point to their line and column.

Runs locally in your browser

Input

18 lines · 321 characters
# docker-compose.yml for local development
x-defaults: &defaults
  restart: unless-stopped
  environment:
    TZ: Europe/Oslo
    COUNTRY: NO

services:
  web:
    <<: *defaults
    image: nginx:1.27
    ports:
      - "8080:80"
  worker:
    <<: *defaults
    image: example/worker:2.4
    command: ["python", "run.py"]

Output

JSON appears here

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

  1. Paste YAML, or open or drop a .yaml or .yml file.
  2. Read the Problems list: a warning marks a value that other YAML tools may read differently, with its line and column.
  3. Choose the Output layout, and how to write a file of Multiple documents.
  4. Copy the JSON or download it: a .json file, or .jsonl for JSON Lines. Swap direction sends the result to JSON to YAML.

Examples

An anchor shared through a merge key, with one value overridden

Input

base: &base
  timeout: 30
  retries: 3
fast:
  <<: *base
  timeout: 5

Output

{
  "base": {
    "timeout": 30,
    "retries": 3
  },
  "fast": {
    "timeout": 5,
    "retries": 3
  }
}
Values that YAML 1.1 reads differently

Input

answer: no
mode: 0644
window: 1:30

Output

{
  "answer": "no",
  "mode": 644,
  "window": "1:30"
}
A CloudFormation tag and a hexadecimal number

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, on and off, in lowercase, capitalized or uppercase;
  • numbers with a leading zero, which are octal in 1.1: 0644 is 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 like 0b101;
  • 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 1e3 and 0o755.

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

Options
OptionValuesDefaultWhen to change it
OutputPretty, 2 spaces, Pretty, 4 spaces, MinifiedPretty, 2 spacesMinified JSON is smallest, for an API request or a fixture; pretty JSON is for reading.
Multiple documentsJSON array, JSON LinesJSON arrayJSON Lines writes one document per line, for log pipelines and jq -c; it applies only to files with several documents.
Resolve merge keys (<<)On, OffOnTurn it off to see << as an ordinary key, for example to check what a file merges.
.inf / .nan valuesError, String, nullErrorString keeps the YAML text, like ".inf"; null is what JavaScript's JSON.stringify writes for these values.

Common errors and fixes

Common errors and how to fix them
CodeWhat it meansHow to fix it
YAML_SYNTAXErrorThe 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_TAB_INDENTErrorA 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_DUPLICATE_KEYErrorA 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_UNRESOLVED_ALIASErrorAn 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_CIRCULAR_ALIASErrorAn 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_ALIAS_LIMITErrorExpanding 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_MERGE_INVALIDErrorThe 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_NUMBER_NOT_JSONErrorThe 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_COMPLEX_KEYErrorA 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_KEY_NOT_STRINGWarningA 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_UNKNOWN_TAGWarningA 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_11_AMBIGUOUSWarningAn 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_DEPTH_LIMITErrorMappings and lists are nested more than 512 levels deep.Flatten the structure; no configuration needs this many levels.

Privacy

YAML to JSON runs entirely in your browser. What you type, paste or open is processed on your device: it is not sent to Toolzami, and Toolzami does not store it. Only the options you choose are remembered, in this browser.

Frequently asked questions

Can I validate a Kubernetes manifest with this?

It checks the YAML syntax and shows exactly what the file contains, but not whether the fields fit a Kubernetes schema. For that, run kubectl apply --dry-run=server against a cluster, or a schema checker such as kubeconform.

What is the difference between .yml and .yaml files?

None: both hold YAML. The YAML project recommends .yaml, while .yml comes from tools that kept three-letter extensions, such as Docker Compose and GitHub Actions. The converter opens either.

Last updated