By Docify

JSON vs YAML: Configuration Files

• 6 min read

JSON uses braces and quoted keys. YAML uses indentation and allows # comments. JSON has no comment syntax. This comparison is not a converter. Convert on /yaml-to-json/ or validate JSON on /json-formatter/.

Both JSON and YAML represent structured data, but they optimize for different use cases. JSON prioritizes simplicity and machine parsing. YAML prioritizes human readability and editing. The right choice depends on who will read and write the file.

Syntax comparison

The same data in both formats:

JSON

{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "lodash": "^4.17.21",
    "express": "^4.18.2"
  },
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  }
}

YAML

# Application configuration
name: my-app
version: "1.0.0"

dependencies:
  lodash: ^4.17.21
  express: ^4.18.2

scripts:
  start: node index.js
  test: jest

YAML is 30% shorter, allows comments, and requires no braces or quotes for simple strings.

Key differences

FeatureJSONYAML
CommentsNot allowed# comments anywhere
Multi-line stringsEscaped \n only| and > blocks
Quotes for stringsAlways requiredOptional for simple strings
Braces/bracketsRequiredIndentation instead
Trailing commasSyntax errorNot applicable
Parse speedFastSlower (more complex grammar)
Anchors & aliasesNot supported&name and *name
Multiple documentsNot supported--- separator

When to use JSON

  • •REST APIs – The standard for HTTP request/response bodies. Every language has fast JSON parsing.
  • •package.json, tsconfig.json – JavaScript ecosystem standards that tools expect.
  • •Data storage – NoSQL databases (MongoDB, CouchDB) store JSON documents.
  • •Machine-generated config – When programs write config, JSON's strictness prevents errors.
  • •Security-sensitive parsing – JSON has no code execution features to exploit.

When to use YAML

  • •Kubernetes manifests – Complex configs with comments explaining each field.
  • •Docker Compose – Service definitions that humans read and edit.
  • •CI/CD pipelines – GitHub Actions, GitLab CI, CircleCI all use YAML.
  • •Ansible playbooks – Infrastructure-as-code with readable structure.
  • •Documentation with examples – OpenAPI specs often use YAML for readability.

Common YAML gotchas

YAML's flexibility can surprise you:

The Norway problem

country: NO # YAML 1.1 parses this as boolean false!

YAML 1.1 treats NO, yes, on, off as booleans. YAML 1.2 Core schema fixes this, but many parsers still default to 1.1. Quote strings when in doubt: "NO".

Indentation sensitivity

A single misaligned space can change structure or cause parse errors. Use an editor with YAML support and consistent indentation (2 spaces is standard).

Implicit typing

version: 1.0 # Parsed as float, not string "1.0"

Numbers, booleans, and null are auto-detected. For version strings, use quotes: "1.0".

Converting between formats

Since YAML 1.2 is a superset of JSON, conversion is straightforward:

  • JSON to YAML: Parse JSON, dump as YAML (comments are not preserved—JSON has none)
  • YAML to JSON: Parse YAML, dump as JSON (comments and anchors are lost)

Use the YAML to JSON converter to convert configs in your browser. The tool uses YAML 1.2 Core schema to avoid boolean surprises.

Convert and validate

Transform YAML to JSON or format JSON. Nothing is uploaded.

FAQ

What is the difference between JSON and YAML?
JSON uses braces and strict quoting. YAML uses indentation and allows comments. Both represent the same data structures.
Does YAML support comments?
Yes. YAML allows # comments. JSON has no comment syntax.
Can I convert between JSON and YAML?
Yes. Use the YAML to JSON or JSON to YAML converter tools. They parse and re-serialize the data.
Which format should I use?
Use JSON for APIs and JavaScript. Use YAML for config files where human editing is common.

Related