JSON vs YAML: Configuration Files
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
| Feature | JSON | YAML |
|---|---|---|
| Comments | Not allowed | # comments anywhere |
| Multi-line strings | Escaped \n only | | and > blocks |
| Quotes for strings | Always required | Optional for simple strings |
| Braces/brackets | Required | Indentation instead |
| Trailing commas | Syntax error | Not applicable |
| Parse speed | Fast | Slower (more complex grammar) |
| Anchors & aliases | Not supported | &name and *name |
| Multiple documents | Not 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
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
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
- YAML 1.2 Core vs 1.1: Norway, Comments, JSON
- How to Format JSON Online: Parse and Prettify
- Validate JSON Syntax with JSON.parse
- CSV to JSON in the Browser with Papa Parse
- Convert JSON to CSV in JavaScript: Papa.unparse vs Papa.parse
- Minify JSON Without Breaking Unicode
- Live tools: YAML to JSON · JSON Formatter · JSON to CSV · CSV to JSON