Comparison
DevOps
YAML
JSON

YAML vs JSON

Verdict: Use YAML for human-authored configuration files — Kubernetes manifests, CI/CD pipelines, Docker Compose, Ansible playbooks. Use JSON for API payloads, machine-generated config, and anywhere strict parsing is required. YAML is more readable but fragile; JSON is verbose but unambiguous.

For a broader DevOps context including DNS, SSL, cron, and HTTP, see the DevOps Configuration & Deployment Basics guide.

Choose YAML when…
  • Writing Kubernetes manifests, Helm charts, or Ansible playbooks
  • Defining CI/CD pipelines (GitHub Actions, GitLab CI, CircleCI)
  • Humans need to read and edit the config regularly
  • You need inline comments to document non-obvious values
  • Multi-line strings or block content (scripts, certificates) need embedding
Choose JSON when…
  • Building or consuming a REST or GraphQL API
  • Config is generated programmatically and never hand-edited
  • Strict validation is required (JSON Schema, JSON Schema validators)
  • The ecosystem is JSON-native (package.json, tsconfig.json, manifest.json)
  • Transmitting data between services or to a browser client

YAML for human-authored config and infrastructure JSON for APIs, data exchange, and machine-generated config.

Side-by-Side Comparison

AspectYAMLJSON
Human readability✓ High — clean syntax, no braces or quotes for simple valuesMedium — readable when pretty-printed, dense when minified
Verbosity✓ Low — no repeated braces, brackets, or quotes for plain valuesHigher — all strings quoted, objects wrapped in braces
Comment support✓ Yes — lines starting with # are commentsNo — comments are not part of the JSON specification
Parsing strictnessFlexible but complex — indentation-sensitive, many implicit type coercions✓ Strict — well-defined grammar, few ambiguities
Schema / validation supportJSON Schema works via YAML-JSON interop. Native YAML schema support is minimal.✓ JSON Schema (community standard). ajv, zod, and most validators target JSON.
Best for config files✓ YAML wins — Kubernetes, Docker Compose, CI/CD, AnsibleAlso used for config (package.json, tsconfig.json) — no comments is the trade-off
Best for APIs / data exchangeRarely used for APIs — not natively supported by most HTTP clients✓ JSON wins — the universal standard for REST, GraphQL, and webhook payloads
Common failure modesTab characters, inconsistent indentation, implicit type coercions, special characters requiring quotesTrailing commas, single-quoted strings, comments, large integers losing precision
Tooling ecosystemStrong in DevOps (kubectl, Helm, Ansible). Limited in web/API ecosystem.✓ Universal — every language has a JSON parser; IDE support is excellent everywhere

What YAML Is

YAML (YAML Ain't Markup Language) is a human-readable serialisation format. The current version is YAML 1.2 (2009). YAML is a superset of JSON — any valid JSON document is also valid YAML 1.2 (with some edge cases). YAML adds:

  • Comments — lines starting with #
  • Unquoted scalars — plain strings without quotes for simple values
  • Block scalars| for literal multi-line strings, > for folded (joined with spaces)
  • Anchors and aliases&anchor and *alias for reusing values (DRY config)
  • Multiple documents in one file, separated by ---
  • Implicit type coercion — bare values like true, null, 1.5 are typed automatically

YAML example — Kubernetes deployment fragment

# Kubernetes Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  labels:
    app: api
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: api
          image: myapp:latest
          env:
            - name: NODE_ENV
              value: production

What JSON Is

JSON (JavaScript Object Notation) is a text-based data format formalised in RFC 8259 (2017). It supports six value types: strings (always double-quoted), numbers, booleans (true/false), null, objects (key-value maps), and arrays. JSON has a deliberately minimal specification — no comments, no trailing commas, no special types like dates or binary.

JSON example — package.json fragment

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "14.2.0",
    "react": "18.3.1"
  }
}

JSON is natively supported in every JavaScript engine via JSON.parse() and JSON.stringify(), and has first-class library support in Python, Go, Rust, Java, and every other mainstream language. This universality is JSON's primary advantage over YAML for data exchange.

Where YAML Is the Better Choice

YAML dominates in human-authored infrastructure configuration because it is significantly more readable than JSON when the structure is deep and values are varied:

Kubernetes and container orchestration
Kubernetes manifests (Deployments, Services, ConfigMaps, Ingress rules) are almost exclusively YAML. Helm charts use YAML templates with Go templating. The Kubernetes API accepts JSON, but virtually all tooling generates and edits YAML.
CI/CD pipelines
GitHub Actions, GitLab CI, CircleCI, Drone, and most pipeline tools use YAML for workflow definitions. The ability to add comments explaining why a step exists is essential for maintainable pipeline files.
Infrastructure as Code
Ansible playbooks, AWS CloudFormation (with YAML), Pulumi configs, and Terraform variable files all support YAML. Comments allow inline documentation of non-obvious configuration choices.
Application configuration files
Spring Boot application.yaml, Ruby on Rails database.yml, Prometheus config.yaml — many application frameworks prefer YAML for their config format because it supports comments and multi-line strings for complex values.
Multi-line string embedding
YAML's literal block scalars (|) and folded block scalars (>) make embedding scripts, certificates, SQL queries, and prose inside config files readable. The JSON equivalent requires escaped newlines (\n) inside a string.
DRY config with anchors
YAML anchors (&name) and aliases (*name) let you define a block once and reuse it — useful for shared environment variable sets across multiple services in a Docker Compose file, or repeated job configurations in a CI pipeline.

Where JSON Is the Better Choice

JSON is the better choice wherever parsing safety, universal tooling support, or machine-generated config are priorities:

  • API payloads. Every REST API returns JSON. HTTP clients, API gateways, and SDK libraries expect JSON request and response bodies. YAML has no practical role in HTTP API communication.
  • Machine-generated config. When a tool generates configuration automatically (a Terraform plan output, an AWS IAM policy, a JSON Schema definition), JSON is safer — there is no risk of generating invalid indentation or accidentally coercing a version string to a float.
  • JavaScript / TypeScript ecosystem config. package.json, tsconfig.json, eslintrc.json, prettierrc.json — the Node.js ecosystem standardised on JSON for project configuration. These files are hand-edited despite JSON's lack of comment support (JSONC — JSON with Comments — is a common workaround).
  • Schema validation with JSON Schema. The JSON Schema ecosystem (Draft 7, 2019-09, 2020-12) has mature validators in every language. YAML can be validated against a JSON Schema by parsing to a JSON-compatible structure first, but native YAML schema support is minimal.
  • Browser and client-side consumption. JSON is natively understood by browsers via JSON.parse(). YAML requires a library (js-yaml, yaml) to be loaded in the browser — an unnecessary dependency when JSON achieves the same goal.

Validation and Parsing Differences

JSON has a strict, unambiguous grammar. Any deviation — a trailing comma, a single-quoted string, a comment — is a parse error. This strictness is a feature: a JSON parser either succeeds completely or fails with a clear error.

YAML is more permissive and more complex. The specification is 80+ pages long. The most common failure modes in YAML are:

  • Tab characters. YAML prohibits tabs for indentation. Most editors insert real tabs unless configured otherwise.
  • Implicit type coercion. Bare values like yes, on, and off are coerced to booleans in YAML 1.1 (used by many parsers). The version string 1.10 is parsed as the float 1.1 in YAML 1.1. Quote these values explicitly.
  • Special characters requiring quotes. A value containing :, #, @, {}, or [] must be quoted — otherwise it is misinterpreted.
  • Inconsistent indentation. Mixing 2-space and 4-space indentation within a file causes parse failures that are hard to locate by eye.

Validate YAML syntax instantly with the YAML Validator — it reports errors with line and column numbers and shows the normalised output.

Common Mistakes

Using a tab character for YAML indentation

YAML only allows spaces. A tab in the indentation produces "found character that cannot start any token" — an unhelpful error that can be hard to spot visually. Configure your editor to expand tabs to spaces for .yaml files.

Relying on YAML boolean coercion for non-boolean values

In YAML 1.1, "yes", "no", "on", "off", "true", and "false" (all case-insensitive) are parsed as booleans. A configuration value like `mode: yes` becomes a boolean true, not the string "yes". Quote string values that look like booleans.

Adding trailing commas to JSON

JSON does not permit trailing commas after the last item in an object or array. Most JavaScript engines accept them, but JSON.parse() rejects them. This is a common mistake when hand-editing JSON configs.

Adding comments to JSON

JSON does not support comments. // and /* */ cause a parse error. If you need comments in a JSON-like config, use JSONC (supported by VS Code and some tools), or switch to YAML.

Mixing YAML indentation levels inconsistently

Using 2 spaces in one part of a file and 4 spaces in another is valid if the relative indentation is consistent, but it causes subtle bugs when copy-pasting sections between files. Use a single indent width (2 or 4 spaces) throughout.

Embedding special characters in YAML without quoting

A database URL like "postgresql://user@host:5432/db" must be quoted in YAML because the @ and : characters have special meaning. Without quotes, parsers may misinterpret the value or fail silently.

Try the Tools

All tools run entirely in your browser. No data is uploaded. Browse all DevOps tools →

Frequently Asked Questions

Related Reading