JSON vs YAML: differences and when to convert

Compare both config formats and when to use YAML or JSON for K8s, Docker Compose, and more.

The same config: API in JSON, K8s in YAML, back and forth in CI — the wrong format leads to parse errors, lost comments, or deployments to the wrong environment.

This article is for full-stack and DevOps engineers. It compares JSON and YAML syntax and selection criteria, describes a safe 5-step conversion workflow, and covers pitfalls like anchors, aliases, and implicit booleans. After reading, you can convert and validate with JSON Toolbox JSON ↔ YAML locally in your browser.

Why understanding JSON and YAML matters

JSON is the de facto standard for APIs; YAML is the de facto language for ops config. Switching between them without knowing the differences risks: "YAML works locally, but after JSON conversion key types changed."

Example from Docker Compose: ports: "8080:8080" vs. numeric ports in JSON, or yes/no in K8s manifests parsed as booleans in YAML — after conversion to JSON, inconsistent client behavior.

What are JSON and YAML

JSON (JavaScript Object Notation) is a strict, text-based exchange format: double-quoted keys, no comments, parser-friendly. YAML (YAML Ain't Markup Language) uses indentation for hierarchy, allows comments and various scalar notations — better for large, hand-maintained configs.

Relationship

In YAML 1.2, JSON is a subset — most valid JSON documents parse directly as YAML. YAML-specific features (anchors &, alias *, multiline |) are lost or must be resolved when converting to JSON.

Key differences compared

Comparison dimensionJSONYAML
Comments❌ Not supported✅ # line comment
Key quotes✅ Required (double)⚠️ Usually optional
HierarchyCurly / square bracketsIndentation (spaces)
API transport✅ Recommended⚠️ Rare
Large hand-written configs⚠️ Many brackets✅ Recommended
StrictnessHigh — parse errors immediatelyRelatively loose — implicit types

Which format for whom

Role / scenarioRecommended formatReason
REST / GraphQL APIJSONUnified ecosystem, unambiguous
Kubernetes / HelmYAMLCommunity convention, commentable
Docker ComposeYAMLOfficial examples and docs
package.json / tsconfigJSONNative toolchain support
Message queue payloadJSONCompact, fast parsing

Typical scenarios — selection guide

  • Frontend/backend API contract: JSON
  • GitHub Actions / GitLab CI (partial steps): YAML
  • Static config before environment variables: team choice — YAML with comments
  • Structure to validate strictly by machine: JSON + JSON Schema

Practice: 5 steps for safe conversion

  1. Set direction: JSON → YAML (readable/editable) or YAML → JSON (API/programs)
  2. Back up the original: keep a copy before conversion
  3. Paste source on the conversion page, choose direction
  4. Review result: validate JSON; for YAML check indentation and types
  5. Smoke test in target environment: deploy or call once — behavior as before

Example: same config in two notations

JSON:

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

YAML:

service: api-gateway
replicas: 3
debug: false
ports:
  - 8080
  - 8443

Typical conversion pitfalls

Implicit YAML types

  • yes / no / on / off may parse as booleans
  • Quote pure number strings, e.g. version: "01"
  • null and ~ in YAML mean empty — becomes null in JSON

Size after JSON → YAML

YAML is often more readable but not always shorter. For transport, JSON + compression is still recommended in production.

Anchors and aliases

YAML &anchor and *alias resolve to duplicated objects on JSON conversion — verify that is intended.

Toolchains: JSON vs. YAML

NeedJSON toolchainYAML toolchain
Browser conversionJSON ToolboxJSON Toolbox
CLI validationjqyamllint / yq
Apply K8sYAML first or CRD JSONkubectl apply -f
Schema constraintsJSON Schema establishedLess uniform standards

Frequently asked questions (FAQ)

Can any JSON be converted to YAML?

Standard JSON yes, semantically equivalent. Key order and indentation style may differ from hand-written YAML — semantics stay the same.

Are YAML comments preserved in JSON?

No. JSON has no comments — they are lost on conversion. Record important notes in docs or README.

Can K8s resources use JSON?

Yes. kubectl supports JSON manifests; the community and Helm usually use YAML — agree on one format for the team.

Most common reasons for conversion errors?

JSON: trailing comma, single quotes. YAML: mixed tabs and spaces, missing space after colon.

Is data uploaded to a server?

No. JSON Toolbox converts locally in the browser — even for internal configs (still remove sensitive values).

Validate again after conversion?

Yes, recommended. At minimum check JSON syntax and smoke-test in staging that the config works.

Conclusion and next steps

JSON favors strictness and interoperability; YAML favors readability and ops friendliness. Rule of thumb: APIs and program-to-program → JSON; large static configs by hand → YAML; always validate and smoke-test on conversion.

Define in the repo which file types need which format, and add format checks in CI — avoid production incidents from implicit YAML types.