JSON vs YAML: Differences, Examples and When to Use Each

JSON and YAML store the same kind of data: keys and values, lists, and objects inside objects. In fact, YAML 1.2 is designed as a superset of JSON, so almost any JSON file is also valid YAML. The difference is who each format is written for. JSON is built for machines to exchange data; YAML is built for people to read and edit by hand.

This guide shows the same data in both formats, compares them side by side, explains when to pick each, and covers the YAML traps that catch even experienced developers.

The quick answer

  • Use JSON for APIs, data sent between programs, browser code, and anything generated by software.
  • Use YAML for configuration files people edit by hand: Docker Compose, Kubernetes, GitHub Actions, Ansible, OpenAPI specs.
  • Need to switch? Convert YAML to JSON (or back) with Doetra for free, in your browser.

The same data in both

Here’s a small app config in JSON:

{
  "app": "doetra-writer",
  "version": 3,
  "debug": false,
  "database": {
    "host": "db.internal",
    "port": 5432
  },
  "languages": ["en", "bn"]
}

And the same thing in YAML:

# Main app settings
app: doetra-writer
version: 3
debug: false
database:
  host: db.internal
  port: 5432
languages:
  - en
  - bn

YAML drops the braces, brackets, quotes and commas, and uses indentation to show nesting. It also allows comments (the line starting with #), which JSON doesn’t.

Side by side

JSONYAML
Structure shown byBraces {} and brackets []Indentation (spaces only, no tabs)
CommentsNot allowedYes, with #
Quotes on stringsAlways double quotesUsually optional
Multi-line textEscaped \n inside one stringBlock styles | and >
Reuse a blockNoAnchors and aliases (&, *)
Several documents per fileNoYes, separated by ---
Parsing speed and safetyFast, simple, very predictableSlower, more complex, more ways to surprise you
Native in browsersYes (JSON.parse)Needs a library
Typical useAPIs, data exchange, storageConfig files, CI pipelines, infrastructure

When to use JSON

  • APIs and webhooks. Nearly every web API sends and receives JSON, and every language can read it without extra libraries.
  • Data written by software. Logs, exports, caches and saved state. Nobody needs to edit them by hand, and strict syntax means fewer surprises.
  • Front-end code. JavaScript reads JSON natively, which makes it the default for anything in the browser.
  • When you need validation. JSON Schema is the most widely used way to describe and check a data shape.

When to use YAML

  • Config files people maintain. Comments let you explain why a setting exists, and the clean layout is easier to review in a pull request.
  • DevOps tools that expect it. Kubernetes manifests, Docker Compose, GitHub Actions, GitLab CI and Ansible playbooks are all YAML.
  • Long text values. Block styles keep multi-line text readable instead of one long escaped string.
  • API specs. OpenAPI accepts both, but most teams write it in YAML because the files get long.

YAML gotchas to watch for

YAML’s flexibility is also where its bugs come from. These trip people up all the time:

  • The “Norway problem”. In YAML 1.1 parsers, unquoted NO, yes, on and off become true/false. A country code list with NO for Norway turns into false. Quote values like these: "NO".
  • Numbers that should be text. zip: 02134 can lose its zero or be read as an octal number, and version: 1.10 becomes 1.1. Quote codes and version strings.
  • Tabs. YAML indentation must use spaces. A single tab causes a parse error that’s hard to spot by eye.
  • Indentation mistakes. One space too many or too few silently moves a key to a different parent. The file is still valid, just wrong.
  • Times and dates. Some parsers turn 12:30 into a number of minutes, or unquoted dates into date objects. Quote them if you need the exact text.

A quick way to catch these: convert the YAML to JSON and read the result. Because JSON is explicit, a value that turned into false or lost its zero stands out immediately.

How to convert between JSON and YAML

  1. Open Doetra’s YAML to JSON converter.
  2. Paste your YAML or JSON, or drop the file. Doetra recognises the format for you.
  3. Choose the output: JSON or YAML. If the input has a syntax error, Doetra points to the line.
  4. Copy or download the result.

Everything runs in your browser, so config files with internal hostnames or keys are never uploaded. Doetra also reads and writes CSV, TSV, XML and TOML, and can generate TypeScript types and JSON Schema from the same data.

One thing to know: comments exist only in YAML. Converting YAML to JSON drops them, so keep the YAML file as your source if the comments matter.

What about TOML?

TOML is a third option for config files, used by Rust (Cargo), Python packaging (pyproject.toml) and Hugo. It’s stricter than YAML, with explicit types and no indentation rules, but it gets clumsy for deeply nested data. If a tool expects TOML, use TOML; Doetra converts it to and from JSON and YAML too.

Frequently asked questions

Is YAML better than JSON?

Neither is better overall. YAML is easier for people to read and edit; JSON is simpler, faster and safer for programs. Most teams use both: JSON for data, YAML for config.

Is JSON valid YAML?

Almost always. YAML 1.2 was designed as a superset of JSON, so a YAML 1.2 parser can read nearly any JSON file.

Why does Kubernetes use YAML?

Manifests are written and reviewed by people, and YAML’s comments and lighter syntax make them easier to maintain. Kubernetes accepts JSON too.

How do I convert YAML to JSON for free?

Paste it into Doetra and choose JSON as the output. No sign-up, and nothing is uploaded.

Try it: convert YAML to JSON, or JSON to YAML →

Scroll to Top