Why convert YAML to JSON?
YAML is great for human-edited configuration, while JSON is the standard for APIs, data interchange and programmatic processing. Converting YAML to JSON lets you feed config files to APIs, validate them against a schema, or inspect their exact structure.
Example conversion
YAML
server:
host: localhost
port: 8080
database:
name: app
replicas:
- db1
- db2JSON
{
"server": { "host": "localhost", "port": 8080 },
"database": {
"name": "app",
"replicas": ["db1", "db2"]
}
}Supported YAML features
| YAML | JSON result |
|---|---|
Mappings (key: value) | Objects |
Sequences (- item) | Arrays |
Inline arrays ([a, b]) | Arrays |
Multi-line strings (| and >) | Strings with line breaks |
true / false | Booleans |
null / ~ | null |
Anchors and aliases (&a / *a) | Expanded values |
Common use cases
- API integration — send a YAML config as a JSON request body
- Kubernetes debugging — see the exact structure of a manifest
- Config validation against a JSON Schema
- Data processing in languages with built-in JSON support
- Generating JSON examples for documentation
YAML gotchas
- The Norway problem: in YAML 1.1 values like
NOandYESare read as booleans. Quote them if you mean text. - Numbers:
1.0becomes the number 1, not the string"1.0"; a zip code such as01234may be read as octal. Quote ambiguous values.
Programmatic conversion
// Node.js (js-yaml)
const yaml = require('js-yaml');
console.log(JSON.stringify(yaml.load(text), null, 2));
# Python (PyYAML)
import json, yaml
print(json.dumps(yaml.safe_load(text), indent=2))
# Command line (yq)
yq -o=json file.yaml
Pro tips
- YAML is whitespace-sensitive — check your indentation if parsing fails.
- JSON doesn't support comments, so YAML comments are dropped.
- Run the result through a JSON validator to double-check it.