Skip to content
Mock and Match

How to convert YAML to JSON (and JSON to YAML)

Convert config files between YAML and JSON, understand what changes (comments, aliases, types), and avoid the most common indentation mistakes.

Published on

When you need to convert

YAML is the go-to format for config files: docker-compose, GitHub Actions workflows, Kubernetes manifests and OpenAPI specs. JSON, meanwhile, dominates APIs and code. It's common to need to move content from one format to the other — whether to test a configuration in a tool that only reads JSON, or to make a JSON more readable as YAML.

Converting YAML to JSON

  1. Open Transform and choose YAML → JSON.
  2. Paste the YAML into the input field.
  3. If you want, adjust the result's indent and click Transform.
Input (YAML)
# Service configuration
service: api
port: 8080
active: true
tags:
  - web
  - internal
database:
  host: localhost
  port: 5432
Output (JSON)
{
  "service": "api",
  "port": 8080,
  "active": true,
  "tags": [
    "web",
    "internal"
  ],
  "database": {
    "host": "localhost",
    "port": 5432
  }
}

Notice the first line's comment doesn't show up in the JSON, and that 8080 and true were recognized as a number and a boolean, with no quotes.

Converting JSON to YAML

The reverse path is the JSON → YAML conversion. You can also choose how many spaces of indent to use (the default is 2).

Input (JSON)
{
  "service": "api",
  "port": 8080,
  "active": true,
  "tags": ["web", "internal"],
  "database": { "host": "localhost", "port": 5432 }
}
Output (YAML)
service: api
port: 8080
active: true
tags:
  - web
  - internal
database:
  host: localhost
  port: 5432

What changes in the conversion

  • Comments: exist only in YAML and are lost when going to JSON.
  • Anchors and aliases (& and *): get expanded, so a reused chunk shows up repeated in the JSON.
  • Types: YAML recognizes numbers, booleans, nulls and dates without quotes. JSON only has text, number, boolean, null, list and object; a date becomes text.
  • Key order: is preserved as in the original.

Common YAML mistakes

  • Tabs in the indentation: YAML only accepts spaces for indenting. A tab causes an error.
  • Inconsistent indentation: items at the same level need the same amount of indent.
  • Colons inside plain text: a value like time: 10:30 can get read in an unexpected way. Wrap it in quotes whenever it contains :, #, or starts with -.

Values that look like something else

Words like no and on, numbers with a leading zero (08), and versions like 1.10 can get interpreted differently by different YAML readers. If it's essential that they stay text, wrap them in quotes.

Frequently asked questions

Is every JSON also valid YAML?

In practice, yes: YAML 1.2 was designed to accept JSON as a subset. That's why converting JSON to YAML never fails because of the format — it only changes the look.

Are comments preserved in the conversion?

No. JSON has no comments, so lines starting with # disappear when converting YAML to JSON. Keep the original YAML if the comments matter.

How do I convert YAML to TOML?

Transform has the YAML → TOML conversion. Keep in mind TOML requires an object at the root, so a YAML that starts with a list can't be converted directly.

Is there a size limit?

The input field accepts up to 10 million characters. On top of that, YAML alias expansion is capped for safety, so a malicious file can't eat up all of the browser's memory.

Try it now

Free, no sign-up, and everything happens in your browser.

Open Transform