Data Formats

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

JSON and YAML represent the same data but feel completely different to work with. JSON uses explicit punctuation and is fast to parse. YAML uses indentation and reads like prose. The right choice depends on who reads the file — humans or machines.

The Same Data, Two Formats

JSON
{
"name": "my-app",
"version": "2.1.0",
"debug": false,
"port": 8080,
"tags": [
"web"
"api"
],
"database": {
"host": "localhost",
"port": 5432
}
}
YAML
name: my-app
version: "2.1.0"
debug: false
port: 8080
tags:
- web
- api
database:
host: localhost
port: 5432
# YAML allows comments here!

Both represent identical data. YAML is ~30% fewer characters and significantly more readable at a glance.

Feature Comparison

FeatureJSONYAML
Comments❌ Not supported✅ Use # character
ReadabilityModerate — punctuation-heavyHigh — minimal punctuation
Parse speed⚡ Very fast (native parsers)Slower — complex grammar
Multi-line stringsAwkward (\n escape chars)✅ Block scalars (| and >)
Data typesstring, number, boolean, null, array, objectSame + timestamps, binary, custom types
Trailing commas❌ Causes parse errorN/A — no commas
Quotes required✅ Always for stringsOptional for simple strings
Indentation significantNo — whitespace ignored✅ Yes — critical
Superset relationshipIndependent spec (RFC 8259)YAML is a superset of JSON
Anchors / aliases❌ No DRY mechanism✅ &anchor and *alias
File extension.json.yaml or .yml
Browser native support✅ JSON.parse()❌ Requires js-yaml or similar
API response standard✅ Dominant formatRarely used for APIs
Config file standardUsed but less common✅ Dominant (K8s, Docker, CI/CD)

Things YAML Can Do That JSON Cannot

1. Comments

# Database configuration
database:
host: localhost # Change this in production
port: 5432

2. Multi-line strings (block scalars)

message: |
This is a multi-line
string that preserves
newlines exactly.
summary: >
This text will be folded
into a single line with spaces.

3. Anchors and aliases (DRY for config)

defaults: &defaults
timeout: 30
retries: 3
development:
<<: *defaults # inherits timeout and retries
debug: true
production:
<<: *defaults
debug: false

Parse Performance

JSON is significantly faster to parse. JavaScript's JSON.parse() is implemented natively in the V8 engine (C++). A YAML parser is implemented in JavaScript and must handle a much more complex grammar.

ScenarioJSONYAML
API response (1MB)~5ms~50–100ms
Config file at startup (10KB)NegligibleNegligible
Batch processing (10,000 files)~1s~10–20s

Estimates for Node.js. For config files read once at startup, the difference is irrelevant. For high-throughput APIs, always use JSON.

When to Use Each

Use JSON when...
  • Building APIs and web services (REST, GraphQL responses)
  • Data interchange between different languages/platforms
  • Storing data in databases (MongoDB, PostgreSQL JSON columns)
  • High-throughput data processing where parse speed matters
  • Browser-based JavaScript (no extra libraries needed)
  • Configuration that is generated by code, not hand-edited
Use YAML when...
  • Writing Kubernetes manifests and Helm charts
  • Docker Compose files
  • CI/CD pipeline definitions (GitHub Actions, GitLab CI, CircleCI)
  • Ansible playbooks and infrastructure-as-code
  • Application config files humans read and edit often
  • Files where comments add important context

YAML Gotchas to Watch Out For

The Norway problem — "no" becomes false

In YAML 1.1, unquoted values like "no", "yes", "on", "off" are parsed as boolean false/true. This broke things when Norway's country code "NO" was stored as false in a config. YAML 1.2 (2009) fixed this, but many parsers still use 1.1 rules. Solution: always quote strings that look like booleans.

country: "NO" # Correct — quoted country: NO # Wrong in YAML 1.1 — parses as false
Tabs are not allowed

YAML uses spaces for indentation. Tab characters cause a parse error, even though they look identical in many editors. Enable "show whitespace" in your editor or use a YAML linter.

Indentation must be consistent

YAML is whitespace-sensitive. Mixing 2-space and 4-space indentation, or accidentally adding/removing a space, causes parse errors or silently changes the structure. Pick 2 spaces and be consistent.

Version numbers need quotes

YAML parses "1.0" as the number 1.0, "1.0.0" as a string, and "1.10" as the number 1.1. To be safe, always quote version strings.

version: "1.0.0" # Correct — string version: 1.0.0 # Also string (3 dots) version: "1.10" # Important: without quotes parses as 1.1

Converting Between JSON and YAML

JavaScript (Node.js)

// YAML → JSON
import yaml from 'js-yaml';
const obj = yaml.load(yamlStr);
const json = JSON.stringify(obj, null, 2);
// JSON → YAML
const obj = JSON.parse(jsonStr);
const yamlStr = yaml.dump(obj);

Python

# YAML → JSON
import yaml, json
obj = yaml.safe_load(yaml_str)
json_str = json.dumps(obj, indent=2)
# JSON → YAML
obj = json.loads(json_str)
yaml_str = yaml.dump(obj)

Note: converting YAML to JSON loses comments, anchors, and aliases — they are YAML-only features with no JSON equivalent.

Frequently Asked Questions

Is YAML a superset of JSON?

Yes, but with caveats. The YAML 1.2 spec states that valid JSON is also valid YAML. In practice, YAML 1.1 parsers (still common) may handle some JSON edge cases differently. For practical purposes: JSON documents can be parsed by YAML parsers, but the reverse is not true (YAML has features JSON doesn't have).

Which is better for Kubernetes?

Kubernetes officially supports both JSON and YAML for manifest files, but the community convention is YAML. All official documentation and examples use YAML. The reason: Kubernetes manifests are written and maintained by humans, and YAML's readability (plus support for comments) makes it much easier to manage complex multi-resource files.

Does JSON support comments?

No. The JSON spec (RFC 8259) explicitly excludes comments. This was deliberate — JSON was designed for data interchange between programs, where comments add no value. If you need comments in a JSON-like format, use JSON5 (a superset that adds comments) or JSONC (used by VS Code and TypeScript config files). Standard JSON parsers will reject both.

Why is YAML indentation critical?

YAML uses indentation to represent data hierarchy instead of braces and brackets. This means a single misplaced space can completely change the structure of the data — moving a value from being a child of one key to a sibling of another, or causing a parse error. Always use a YAML validator to check your files before deploying.

Can I use YAML for REST APIs?

Technically yes — HTTP doesn't care about the body format as long as you set Content-Type correctly. But in practice, don't. All major HTTP clients and frameworks expect JSON for APIs. JSON has native browser support, faster parsers, and is the universally expected format for web APIs. Use YAML for configuration files, not API responses.

Key Takeaways

  • JSON is for machines; YAML is for humans. That's the core difference.
  • YAML supports comments and multi-line strings; JSON does not.
  • JSON is 10–20x faster to parse — always use JSON for APIs.
  • YAML is the standard for Kubernetes, Docker Compose, GitHub Actions, and Ansible.
  • YAML 1.1's Norway problem: always quote strings that look like booleans (yes, no, on, off).
  • YAML is a superset of JSON — valid JSON is valid YAML, but not vice versa.

Related Resources