YAML to JSON

Convert YAML to JSON format

About YAML to JSON

YAML to JSON parses pasted YAML with js-yaml, using the YAML 1.2 rules for booleans and null, then writes JSON indented by two spaces. Mappings become objects and - item lists become arrays; anchors and aliases are expanded and merge keys are applied; several --- documents become one JSON array. Only true and false are booleans, so yes and no stay strings, and dates stay text. Integers keep every digit, but decimals become JSON numbers: an unquoted 1.10 is written 1.1. Custom tags such as !Ref are not supported. js-yaml does all of this inside your browser tab.

Example

A configuration with an anchor, a merge key, an unquoted and a quoted version, and the JSON the tool returns:

YAML

defaults: &base
  retries: 3
  debug: no
prod:
  <<: *base
  host: example.com
  version: 1.10
  tag: "1.10"
  build: 0012

JSON

{
  "defaults": {
    "retries": 3,
    "debug": "no"
  },
  "prod": {
    "retries": 3,
    "debug": "no",
    "host": "example.com",
    "version": 1.1,
    "tag": "1.10",
    "build": 12
  }
}

How to convert YAML to JSON

  1. 1Paste YAML into "YAML input".
  2. 2Click "Convert".
  3. 3Read the JSON in "JSON output", or the error, with its line and column when the YAML syntax is wrong.
  4. 4Click "Copy" for the clipboard, or "Download" for a data.json file.

Formats and limits

Input
YAML text, one or more documents
Output
JSON (two-space indent), saved as data.json
Not supported
custom tags (!Ref, !Sub), !!binary, !!timestamp
Refused values
.inf and .nan, which JSON cannot hold

Common uses

Reading Kubernetes manifests in a script
A manifest file with a Deployment, a Service, and an Ingress separated by --- becomes one JSON array with one element per resource, ready for jq or a script that only reads JSON.
Seeing what a Compose file really sets
Converting a docker-compose.yml that reuses &defaults with <<: *defaults writes each service with the merged keys spelled out, so the final environment, ports, and restart policy of every service can be read directly.
Moving a config to a JSON-only tool
An OpenAPI description or a CI pipeline written in YAML becomes JSON for a validator, a REST client, or a library that only accepts JSON, with long integers kept digit for digit.

Which tool to use

  • JSON to YAML: the reverse direction; it puts strings such as yes, 123, or a date in single quotes so a YAML reader keeps them as text.
  • TOML to JSON: for Cargo.toml and pyproject.toml files; it writes TOML dates as ISO 8601 strings and turns inf and nan into null with a notice, where this tool refuses .inf and .nan.
  • JSON Formatter: to sort the keys of the result A–Z, switch to 4 spaces or a tab, or minify it, since this tool always writes JSON indented by two spaces.

Where your file is processed

js-yaml runs in your browser to read the YAML, and the content you paste is not uploaded, so configuration files with internal host names stay with you. When an error occurs, its message with quoted fragments removed is reported to us, along with the tool name and your browser and its version.

Frequently asked questions

Is a version like 1.10 kept?

No, unless it is quoted. Unquoted, 1.10 is a YAML number and is written 1.1 in JSON; in quotes it stays the string 1.10, as in the example. In the same way 0012 becomes 12.

Are yes and no converted to booleans?

No. Under YAML 1.2, which this tool follows, only true and false are booleans (True and TRUE count too), so yes, no, on, and off stay strings. Write true or false if you need booleans.

Does it support anchors and merge keys?

Yes. An alias such as *base copies the anchored value, and <<: *base merges its keys into the mapping, as Docker Compose and GitLab CI files use it; keys written next to the merge win.

Can I convert a CloudFormation template?

No, not one that uses short tags. !Ref, !Sub, and other custom tags stop the conversion with an unknown tag error, as do !!binary and !!timestamp. Templates written with the long form, such as Ref: MyBucket, convert normally.

Are YAML comments kept?

No. JSON has no comments, so every # comment is dropped, whether it sits on its own line or after a value.

What happens when a key appears twice in the same mapping?

The conversion stops with a duplicated mapping key error, with its line and column, instead of silently keeping one of the two values. Remove or rename one of them and convert again.

Why do I get an error about tab characters?

YAML does not allow tabs in indentation. Replace them with spaces; the error names the line and column of the first tab found.

How are multi-line | and > strings converted?

A | block keeps its line breaks and a > block joins its lines with spaces; both become one JSON string ending with a single line break, such as line one\nline two\n.

What do an empty value and ~ become?

null. A key with nothing after its colon, ~, and null all give JSON null, and a document that holds only comments converts to null as well.