<!-- YAML vs JSON: Differences and When to Use Each -->
<!-- Canonical: https://dataswitchpro.com/guides/yaml-vs-json/ -->
> YAML and JSON compared: syntax, comments, readability and pitfalls such as the Norway problem, and which to use for config files and APIs.

[Home](https://dataswitchpro.com/) › [Guides](https://dataswitchpro.com/guides/)

# YAML vs JSON: Differences, Pitfalls and When to Use Each

By the DataSwitch Pro team · November 20, 2026 · 3 min read

## Quick answer

YAML and JSON can represent the same data — YAML 1.2 is even a superset of JSON — but YAML uses indentation instead of braces, allows comments and is easier for people to read and edit, while JSON is stricter, simpler to parse and less prone to ambiguity. Use YAML for configuration files that humans maintain (Kubernetes, Docker Compose, GitHub Actions) and JSON for APIs, data exchange and files written by programs.

Open a Kubernetes manifest and you see YAML; call almost any web API and you get JSON. Both describe nested maps, lists, strings, numbers and booleans. The choice between them is about who reads and writes the file, and how much ambiguity you can tolerate.

## The same configuration in both

### YAML

```
# web service
service:
  name: api
  replicas: 3
  ports:
    - 8080
    - 8443
  debug: false
```

### JSON

```
{
  "service": {
    "name": "api",
    "replicas": 3,
    "ports": [8080, 8443],
    "debug": false
  }
}
```

## Key differences

- **Structure.** YAML uses indentation (spaces only, never tabs); JSON uses braces, brackets and commas.
- **Comments.** YAML supports `# comments`; JSON does not, which is the main reason JSON is unpopular for hand-edited config.
- **Quotes.** Most YAML strings need no quotes. JSON requires double quotes around every string and key.
- **Reuse.** YAML has anchors and aliases (`&defaults`, `*defaults`, `<<:`) to avoid repetition. JSON has none.
- **Multiple documents.** One YAML file can hold several documents separated by `---`. A JSON file is one value.
- **Multi-line text.** YAML has block scalars (`|` keeps line breaks, `>` folds them), handy for scripts and certificates. JSON needs `\n` escapes.
- **Parsing and safety.** JSON parsers are small, fast and identical everywhere. YAML parsers are complex, differ between YAML 1.1 and 1.2, and some can create objects from tags — always use a “safe load” function for untrusted YAML.

## YAML pitfalls to know

- **Implicit types.** Unquoted values are typed automatically. In YAML 1.1 parsers, `yes`, `no`, `on` and `off` become booleans — the famous “Norway problem”, where the country code `NO` turns into `false`.
- **Numbers that are not numbers.** `version: 1.10` becomes the number 1.1, and `zip: 02134` may lose its zero or be read as octal in older parsers. Quote them.
- **Indentation errors** silently change the structure: a key indented one level too far belongs to a different parent. See [common YAML errors and how to fix them](https://dataswitchpro.com/guides/fix-yaml-errors/).
- **Colons in values.** `title: Note: read this` needs quotes.

## When to use YAML

- Configuration that people edit by hand: Docker Compose, Kubernetes, Ansible, GitHub Actions, OpenAPI specs.
- Files that benefit from comments explaining each setting.
- Content front matter in static site generators.

## When to use JSON

- APIs, webhooks and data sent between programs.
- Files generated and read by software, where strictness prevents surprises.
- Browser code, where `JSON.parse` is built in.
- Large data sets — JSON parsing is significantly faster.

## Converting between them

Because every JSON document is valid YAML 1.2, converting JSON to YAML is safe: a good converter quotes strings such as `"yes"` or `"02134"` so they keep their meaning. Converting YAML to JSON is also a quick way to check what a YAML file really means — the JSON shows exactly which values became numbers, booleans or nested objects. Use [JSON to YAML](https://dataswitchpro.com/json-to-yaml/) and [YAML to JSON](https://dataswitchpro.com/yaml-to-json/). If you are weighing a third option, read [TOML vs YAML vs JSON](https://dataswitchpro.com/guides/toml-vs-yaml-vs-json/).

**Try it on your own file.** Free, private, and processed in your browser.

[JSON to YAML](https://dataswitchpro.com/json-to-yaml/)[YAML to JSON](https://dataswitchpro.com/yaml-to-json/)[JSON Formatter](https://dataswitchpro.com/json-formatter/)[JSON to TOML](https://dataswitchpro.com/json-to-toml/)

## Frequently asked questions

### Is JSON valid YAML?

Yes for YAML 1.2, with very rare edge cases. Most YAML parsers can read a JSON file directly.

### Which is faster to parse?

JSON, by a wide margin, because its grammar is much simpler.

### Are comments kept when converting YAML to JSON?

No. JSON has no comments, so they are dropped.

### Should I use .yml or .yaml?

Both work. `.yaml` is the officially recommended extension; many tools such as Docker Compose accept either.

## More guides

- [Common YAML Errors and How to Fix Them](https://dataswitchpro.com/guides/fix-yaml-errors/)
- [TOML vs YAML vs JSON for Configuration Files](https://dataswitchpro.com/guides/toml-vs-yaml-vs-json/)
- [Invalid JSON? 10 Common Syntax Errors and How to Fix Them](https://dataswitchpro.com/guides/fix-invalid-json-errors/)
- [How to Flatten Nested JSON into a CSV Table](https://dataswitchpro.com/guides/flatten-nested-json-to-csv/)
