JSON vs YAML - Ktery format dat pouzit?

Podrobne srovnani formatu JSON a YAML. Zjistete, kdy pouzit JSON a kdy YAML, rozdily v syntaxi, vykonu a citelnosti.

Porovnani JSON a YAML

Porovnani obou moznosti podle klicovych vlastnosti.

Vlastnost JSON YAML
Readability Good Excellent
Parsing Speed Very Fast Slower
File Size Larger (brackets) Smaller (indent)
Comments Support No Yes
Language Support Universal Growing
Security Risk Low Higher (YAML bombs)
Schema Validation Strong (JSON Schema) Limited
Learning Curve Easy Moderate
Nejlepsi Stredni Slabe ✓ = vitez v radku

Kdy pouzit - shrnuti

JSON

API, vymena dat mezi sluzbami a konfiguracni soubory konzumovane JavaScriptem.

YAML

Konfigurace psana lidmi (Docker Compose, Kubernetes), CI/CD pipeline a dokumentace s komentari.

Kdy pouzit jednotlive formaty

Kdy pouzit JSON

  • REST API request and response bodies
  • Configuration files consumed by JavaScript apps
  • NoSQL database storage (MongoDB, DynamoDB)
  • Data exchange between microservices and web hooks
  • Package manifests like package.json and tsconfig.json

Kdy pouzit YAML

  • Docker Compose and Kubernetes manifest files
  • CI/CD pipeline definitions (GitHub Actions, GitLab CI)
  • Human-edited configuration with comments (Ansible, Prometheus)
  • Documentation front matter (Jekyll, Hugo, MkDocs)
  • CloudFormation and Serverless Framework templates

Casto kladene otazky

Is YAML a superset of JSON?
No, this is a common misconception. While YAML 1.2 was designed to allow JSON to be used as a valid subset, the two parsers differ in practice. JSON is always valid YAML only under specific YAML 1.2 parsers, and many real-world YAML libraries follow YAML 1.1 semantics, which do not accept arbitrary JSON.
Which format is faster to parse, JSON or YAML?
JSON is significantly faster to parse in nearly every language runtime. JSON parsers rely on simple delimiters such as braces and quotes, while YAML must interpret significant whitespace, anchors, aliases, and multi-document streams. Benchmarks routinely show JSON parsing 3-10x faster than YAML for equivalent payloads.
Can I add comments to JSON?
Standard JSON (RFC 8259) does not support comments. Common workarounds include using a dedicated key such as "_comment" or switching to JSONC, JSON5, or HJSON supersets. YAML supports comments natively with the # character, which is one of its main advantages for hand-edited configuration.
What is a YAML bomb and how do I avoid it?
A YAML bomb (billion laughs attack) uses YAML anchors and aliases to create exponential expansion of a small file, exhausting memory. To avoid it, use a parser that disables alias expansion or caps document depth, and never parse untrusted YAML input. JSON is not vulnerable to this class of attack.
Should I use JSON or YAML for Kubernetes manifests?
Kubernetes accepts both, but YAML is the community standard because it supports comments, multi-document files via ---, and is more compact for hand editing. JSON works well for machine-generated manifests and API calls using kubectl apply -f.

Souvisejici nastroje