YAML to JSON Converter — Convert YAML Configuration to JSON
YAML to JSON conversion is lossless for most data types. Here's how the conversion works, what YAML features don't survive the conversion, and how to do it in code.
YAML to JSON conversion takes YAML’s human-friendly syntax (indentation-based hierarchy, no quotes required) and outputs the equivalent JSON structure (curly braces, quoted keys, explicit commas). Both formats represent the same data model — objects, arrays, strings, numbers, booleans, null.
Use the YAML to JSON converter to paste YAML and get formatted JSON output instantly.
What YAML to JSON looks like
# YAML input
name: Alice
age: 30
active: true
address:
street: "123 Main St"
city: Springfield
zip: "01234"
tags:
- developer
- python
- remote
Converts to:
{
"name": "Alice",
"age": 30,
"active": true,
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "01234"
},
"tags": ["developer", "python", "remote"]
}
The data is identical. YAML’s indentation becomes JSON’s nested braces/brackets.
YAML features that don’t survive conversion
Some YAML features have no JSON equivalent and are lost in conversion:
Comments
# This is a comment — YAML supports them
name: Alice # inline comment too
Comments are stripped. JSON has no comment syntax.
Multiple documents in one file
YAML supports multiple documents separated by ---:
---
name: Alice
---
name: Bob
JSON doesn’t support multiple documents in one file. Converters typically output only the first document, or output a JSON array containing all documents.
YAML anchors and aliases
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com
Anchors (&name) and aliases (*name) allow reuse. After conversion, the alias is expanded — the output JSON contains the full expanded values, not references:
{
"defaults": {"timeout": 30, "retries": 3},
"production": {"timeout": 30, "retries": 3, "host": "prod.example.com"}
}
Complex keys
YAML allows non-string keys (numbers, booleans, even objects as keys). JSON requires string keys. Converters typically stringify non-string keys.
42: "numeric key"
true: "boolean key"
{"42": "numeric key", "true": "boolean key"}
Type coercion quirks
YAML infers types from values without quotes:
port: 8080 # integer
ratio: 1.5 # float
enabled: true # boolean
name: "yes" # quoted → string
bare_yes: yes # YAML 1.1: boolean! YAML 1.2: string
nothing: ~ # null
nothing2: null # null
The yes/no/on/off issue: In YAML 1.1 (used by most parsers including PyYAML), bare yes, no, on, off, true, false are all booleans. This causes bugs in config files:
# YAML 1.1: Norway problem
countries:
- GB
- US
- NO # Parsed as boolean false! (NO = false in YAML 1.1)
- FR
Always quote values that could be misinterpreted: "NO", "yes", "on".
How to convert YAML to JSON in code
Python
import yaml
import json
# Convert YAML string to JSON:
yaml_text = """
name: Alice
age: 30
active: true
"""
data = yaml.safe_load(yaml_text)
json_output = json.dumps(data, indent=2)
print(json_output)
# Convert YAML file to JSON file:
with open('config.yaml') as f:
data = yaml.safe_load(f)
with open('config.json', 'w') as f:
json.dump(data, f, indent=2)
Node.js
const yaml = require('js-yaml');
const fs = require('fs');
// Convert YAML string:
const yamlText = `
name: Alice
age: 30
active: true
`;
const data = yaml.load(yamlText);
const jsonOutput = JSON.stringify(data, null, 2);
console.log(jsonOutput);
// Convert file:
const doc = yaml.load(fs.readFileSync('config.yaml', 'utf8'));
fs.writeFileSync('config.json', JSON.stringify(doc, null, 2));
Command line
# Using yq (popular YAML processor):
yq -o=json config.yaml
# Using Python one-liner:
python3 -c "import yaml,json,sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" < config.yaml
# With prettier (Node.js):
npx yaml-to-json config.yaml
When to convert YAML to JSON
Interoperability: Many APIs, tools, and runtimes accept JSON but not YAML. Kubernetes configs are YAML; the Kubernetes API communicates in JSON. When submitting configs programmatically, you often need JSON.
Performance: JSON is faster to parse than YAML. For high-frequency config reads (service startup), converting YAML to JSON at build time and shipping JSON can improve startup time.
Validation: JSON Schema validators are more mature and widely available than YAML validators. Converting to JSON lets you validate with standard JSON Schema tools.
Debugging API requests: If your application reads YAML and sends it to an API, the API likely receives JSON. Converting at the CLI level lets you inspect exactly what the API will receive.
JSON to YAML (reverse direction)
The reverse conversion — JSON to YAML — is also lossless (JSON is a subset of YAML 1.2). Any valid JSON is valid YAML.
import yaml, json
json_text = '{"name": "Alice", "age": 30}'
data = json.loads(json_text)
yaml_output = yaml.dump(data, default_flow_style=False)
print(yaml_output)
# age: 30
# name: Alice
Note: yaml.dump sorts keys alphabetically by default. Pass sort_keys=False to preserve the original order.
Related tools
- YAML to JSON — convert YAML configs to JSON
- YAML vs JSON — when to use each format
- JSON Formatter — validate and format your JSON output
Related posts
- YAML vs JSON: Which to Use When (and Why It Matters) — JSON is for machines, YAML is for humans, and choosing the wrong one quietly cos…
- YAML Anchors and Aliases — Reusing Values with & and * — YAML anchors (&) define a reusable value; aliases (*) reference it. This elimina…
- YAML Config Best Practices — Structure, Validation, and Environment Variables — YAML is the dominant format for configuration files in modern software. Here's h…
- The 10 Most Common JSON Validation Errors (and How to Fix Them) — Every JSON parse error in production traces back to one of ten root causes. This…
Related tool
Convert between YAML and JSON formats with full fidelity.
Written by Mian Ali Khalid. Part of the Data & Format pillar.