Tham chiếu Cập nhật lần cuối: 2026-11-23
So Sánh Định Dạng Cấu Hình: JSON, YAML, TOML, .env & XML
So sánh trực tiếp JSON, YAML, TOML, .env và XML về chú thích, kiểu dữ liệu, chuỗi nhiều dòng, dấu phẩy cuối, bảo mật và ứng dụng phù hợp.
| Dimension | JSON | YAML | TOML | .env | XML |
|---|---|---|---|---|---|
| Comments | No (unless JSONC/JSON5) | Yes (# line comments) | Yes (# line comments) | Yes (# line comments) | Yes (<!-- comment -->) |
| Native Data Types | string, number, boolean, null, array, object | string, int, float, bool, null, timestamp, list, map | string, int, float, bool, datetime, array, table | Strings only (consumers parse bool/int manually) | Text nodes & attributes (schema adds types) |
| Multiline Strings | Escaped \n only (single line) | Native literal (|) and folded (>) blocks | Native triple quotes ("""...""" / '''...''') | Limited (quoted \n or multi-line in newer parsers) | Native multiline text & CDATA sections |
| Trailing Commas | Forbidden (strict RFC 8259) | N/A in block style; disallowed in strict YAML flow collections | Allowed in multiline arrays (forbidden in inline tables) | N/A (line-delimited key=value) | N/A (tag-delimited) |
| Whitespace Sensitivity | Insignificant outside strings | Strict indentation (spaces only; tabs break) | Insignificant indentation | Sensitive around unquoted values & spaces around = | Insignificant between tags; preserved in elements |
| Quoted Keys Required | Always double-quoted | Optional for bare keys (quote special/numeric keys) | Optional for bare A-Za-z0-9_- keys | Keys unquoted (UPPER_SNAKE_CASE) | Tag and attribute names unquoted; attribute values quoted |
| Nested Hierarchy | Arbitrary depth with {} and [] | Arbitrary depth via indentation | Explicit [section.sub] tables & array-of-tables [[items]] | Flat only (hierarchy simulated via KEY_PREFIX__SUB) | Deep element trees + attributes |
| Type Coercion Pitfalls | None (explicit quotes differentiate strings) | YAML 1.1 turns yes/no/on/off/NO into booleans; octal 012 | Explicit syntax prevents implicit coercion | Everything is string until app parses "false" -> true pitfall | Untyped text unless XSD applied |
| Anchors & Reuse | None | Yes (&anchor and *alias + << merge) | None (intentionally omitted for clarity) | Variable expansion ($VAR) in dotenv-expand | XML Entities (&name;) & XInclude |
| Date & Time Literals | None (stored as ISO 8601 strings) | Native timestamp scalars (risk of unexpected Date objects) | First-class RFC 3339 offset/local date-time types | Strings only | ISO strings via XSD xs:dateTime |
| Null Representation | null | null, ~, or empty value | No null type (omit optional keys instead) | Empty string VAR= (not true null) | Empty tag or xsi:nil="true" |
| Multiple Docs per File | No (use NDJSON/JSONL for streams) | Yes (--- document separators) | No | No | Single root element required |
| Schema Standard | JSON Schema (Draft 2020-12) | JSON Schema (via YAML->JSON mapping) | No universal standard (Taplo / JSON Schema) | Runtime validators (Zod, envalid, Pydantic Settings) | XSD, DTD, RelaxNG |
| Parsing Speed | Fastest (native C/SIMD & V8 JSON.parse) | Slowest (80+ page spec, complex lexer) | Fast (simple unambiguous grammar) | Instant (trivial line split) | Moderate (SAX/DOM parsers heavier than JSON) |
| Security Risks | Minimal (watch deep nesting / __proto__ in JS) | Billion laughs anchor DoS; unsafe loaders (!!python/object) | Minimal (no code execution tags or anchors) | Command injection if sourced directly in shell scripts | XXE (External Entity) & Billion Laughs entity expansion |
| Diff & Git Review Ergonomics | Noisy trailing comma & brace diffs | Clean line diffs, indentation shifts can hide scope | Very clean flat section diffs with explicit headers | Clean line-by-line diffs (never commit secrets!) | Verbose opening/closing tag noise |
| Human Hand-Editing | Fair (strict punctuation, no comments) | High for shallow files; error-prone at deep nesting | Highest for app/CLI configuration | Highest for flat secrets & runtime flags | Low (verbose closing tags) |
| Standard Extensions | .json (application/json) | .yaml, .yml (application/yaml) | .toml (application/toml) | .env, .env.local (text/plain) | .xml (application/xml) |
| Best Use Cases | REST/GraphQL APIs, lockfiles, machine interchange | Kubernetes, Docker Compose, GitHub Actions, OpenAPI | Cargo.toml, pyproject.toml, Hugo, CLI tool configs | 12-Factor runtime secrets, API keys, environment overrides | SOAP, SAML, Maven pom.xml, RSS/Atom, SVG, sitemaps |
| Ecosystem Adoption | Universal across browsers, servers, and NoSQL DBs | Dominant in cloud-native & DevOps tooling | Standard in Rust, Python packaging, and Go/Hugo CLIs | Universal for local dev & container env injection | Enterprise integrations, publishing, and web standards |
No matching rows found
Try another search term or clear the filter to restore all rows.
Công cụ liên quan
Hướng dẫn liên quan
Tài liệu liên quan
Câu hỏi thường gặp
- When should I choose TOML or YAML instead of JSON for configuration?
- Choose JSON for machine-to-machine APIs and strict schemas, YAML for Kubernetes, Docker Compose, or CI workflows needing multi-document streams, and TOML for human-edited application configs free of indentation pitfalls.
- What is the YAML 1.1 "Norway bug" type coercion pitfall?
- In YAML 1.1, unquoted scalars such as yes, no, on, off, and NO (Norway country code) are implicitly parsed as booleans. Always quote ambiguous string values or use JSON/TOML where types are explicit.
- Why should .env files never be committed or sourced directly in shell scripts?
- .env files hold runtime secrets and environment overrides that should stay out of Git history, and sourcing untrusted .env lines in a shell risks command injection—parse them with a dedicated dotenv loader instead.