JSON ↔ YAML Converter/ JSON ↔ YAML
Bidirectional conversion between JSON and YAML — switch directions instantly with custom JSON indentation.
JSON
YAML
Did this tool solve your problem?
Code Examples
JavaScript (js-yaml)
// npm install js-yaml
import yaml from 'js-yaml';
// JSON → YAML
const obj = { name: 'Alice', scores: [100, 95] };
const yamlStr = yaml.dump(obj, { indent: 2 });
// name: Alice
// scores:
// - 100
// - 95
// YAML → JSON
const yamlInput = `
name: Alice
scores:
- 100
- 95
`;
const parsed = yaml.load(yamlInput);
JSON.stringify(parsed, null, 2);
// { "name": "Alice", "scores": [100, 95] }Python (PyYAML)
# pip install pyyaml
import yaml, json
# JSON → YAML
data = {"name": "Alice", "scores": [100, 95]}
yaml_str = yaml.dump(data, default_flow_style=False,
allow_unicode=True, indent=2)
# name: Alice
// scores:
// - 100
// - 95
# YAML → JSON
with open('config.yaml') as f:
data = yaml.safe_load(f) # use safe_load!
json_str = json.dumps(data, indent=2, ensure_ascii=False)
# Stream large files
for doc in yaml.safe_load_all(open('multi.yaml')):
print(json.dumps(doc))Go
// go get gopkg.in/yaml.v3
import (
"encoding/json"
"gopkg.in/yaml.v3"
)
// YAML → JSON
yamlStr := []byte("name: Alice\nage: 30")
var data map[string]interface{}
yaml.Unmarshal(yamlStr, &data)
jsonBytes, _ := json.MarshalIndent(data, "", " ")
// JSON → YAML
jsonStr := []byte(`{"name":"Alice","age":30}`)
var obj interface{}
json.Unmarshal(jsonStr, &obj)
yamlBytes, _ := yaml.Marshal(obj)
// age: 30
// name: AliceCLI Tools
# yq: YAML/JSON Swiss Army knife
# brew install yq / apt install yq
# YAML → JSON
yq -o=json config.yaml
# JSON → YAML
yq -P config.json
# In-place conversion
yq -i -o=yaml config.json
# Python one-liner: JSON → YAML
python3 -c "
import sys, json, yaml
print(yaml.dump(json.load(sys.stdin),
default_flow_style=False, allow_unicode=True))
" < config.jsonFrequently Asked Questions
What is the relationship between YAML and JSON?
YAML is a superset of JSON — valid JSON is valid YAML. YAML adds: comments (#), multi-line strings (| and >), anchors and references (& and *), flexible date/time types, and unquoted strings. YAML is preferred for configuration files; JSON is better for data exchange.
When should I use YAML instead of JSON?
Use YAML for: CI/CD configs (GitHub Actions, GitLab CI), Docker Compose, Kubernetes manifests, config files that humans edit and maintain, content with multi-line text. Use JSON for: API data exchange (faster), strict type requirements, machine-generated data.
What are YAML's indentation rules?
YAML uses spaces (never tabs) for indentation. All keys at the same level must have identical indentation. Standard is 2 or 4 spaces. YAML is very sensitive to indentation errors — one extra space can change the data structure or cause a parse error.
How do I write multi-line strings in YAML?
Two block styles: literal block (|) preserves newlines — each line break is kept as-is. Folded block (>) replaces single newlines with spaces (blank lines become newlines) — good for long paragraphs. Flow styles: single quotes (no escape processing), double quotes (handles escapes).
How do I parse YAML in Node.js?
Use the js-yaml library (npm install js-yaml): const yaml = require('js-yaml'); const obj = yaml.load(yamlString); const str = yaml.dump(obj);. For security, pass a safe schema: yaml.load(str, { schema: yaml.JSON_SCHEMA }) to prevent execution of JavaScript constructors.
What are common compatibility issues when converting?
1) YAML comments (#) are lost when converting to JSON. 2) YAML anchors (&) and aliases (*) are expanded into full objects in JSON. 3) YAML date values are parsed as Date objects. 4) YAML integers become numbers; very large integers may lose precision in JSON.