The JSON Output
Converting the example YAML produces:
{
"server": {
"host": "0.0.0.0",
"port": 8080,
"ssl": true
},
"database": {
"url": "postgresql://localhost:5432/myapp",
"pool_size": 10,
"timeout": 30
},
"logging": {
"level": "info",
"format": "json",
"outputs": [
"stdout",
"file"
]
}
}
The indentation hierarchy becomes nested JSON objects. YAML sequences become JSON arrays. Unquoted YAML strings that are not booleans or numbers become JSON strings.
What Converts Cleanly
Most YAML maps cleanly to JSON with no information loss:
| YAML type | JSON equivalent |
|---|---|
Mapping (key: value) | Object ({"key": "value"}) |
Sequence (- item) | Array (["item"]) |
| String | String |
| Integer | Number |
| Float | Number |
Boolean (true/false) | Boolean |
Null (null, ~, empty) | null |
For a typical Kubernetes manifest or Docker Compose file, the conversion is lossless from a data perspective.
What Does Not Survive
Comments
YAML comments (lines starting with # or inline # comment suffixes) are stripped entirely. JSON has no comment syntax. If your YAML config uses comments to document fields, those explanations disappear in the JSON output. There is no workaround other than moving documentation out of comments and into a separate schema or README.
Anchors and Aliases
YAML allows defining a reusable block with an anchor and referencing it elsewhere with an alias:
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com
staging:
<<: *defaults
host: staging.example.com
In the JSON output, *defaults is fully expanded in both production and staging. The anchor notation is gone, and the shared values are duplicated:
{
"defaults": { "timeout": 30, "retries": 3 },
"production": { "timeout": 30, "retries": 3, "host": "prod.example.com" },
"staging": { "timeout": 30, "retries": 3, "host": "staging.example.com" }
}
YAML Tags
Tags like !!binary, !!timestamp, and !!python/object are YAML-specific type annotations. Most converters either coerce the value to a string or raise an error. !!timestamp values like 2024-01-15 may become strings, numbers (Unix epoch), or cause an error depending on the library.
When You Need This Conversion
Kubernetes API Calls
Kubernetes stores resources internally as JSON, and its REST API accepts JSON. When calling the Kubernetes API directly (rather than using kubectl), you need JSON. If your manifest is in YAML, convert it first:
# kubectl can convert for you
kubectl apply -f deployment.yaml --dry-run=client -o json
Or use a dedicated tool for scripted pipelines.
Importing YAML Config into JavaScript or TypeScript
Node.js has no built-in YAML parser. Rather than adding a YAML dependency, some teams store config as JSON. Converting once at build time or checking in the JSON alongside the YAML is a practical option for simple cases.
Debugging YAML by Viewing as JSON
JSON’s explicit braces and quotes make structure unambiguous. YAML’s indentation can be deceptive: a misplaced space changes which block a key belongs to. Converting to JSON and checking the structure is a quick way to verify that your YAML actually parses the way you think it does. This is particularly useful when debugging Helm chart values or GitHub Actions workflow files where indentation errors cause confusing behavior.