How to Convert JSON to YAML (and YAML to JSON)
Learn how to convert between JSON and YAML formats. Free online converter for configuration files, Kubernetes manifests, and more.
JSON and YAML are both popular data serialization formats. This guide shows you how to convert between them and when to use each format.
JSON vs YAML at a Glance
| Feature | JSON | YAML |
|---|---|---|
| Readability | Good | Excellent |
| Comments | No | Yes |
| File size | Larger | Smaller |
| Parsing speed | Faster | Slower |
| Use case | APIs, data | Config files |
How to Convert JSON to YAML Online
Step 1: Open jsoneditor.io
Go to jsoneditor.io in your browser. No signup needed.
Step 2: Paste Your JSON
Enter your JSON data in the editor. The tool validates it automatically and shows any errors.
Step 3: Convert to YAML
Click the Convert dropdown and select JSON to YAML. Your data is instantly converted:
JSON input:
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-service"
},
"spec": {
"ports": [
{
"port": 80,
"targetPort": 8080
}
]
}
}YAML output:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
targetPort: 8080How to Convert YAML to JSON
The process works in reverse too:
- Paste your YAML into the editor
- Click Convert > YAML to JSON
- Copy or download the JSON output
This is useful when you need to:
- Send YAML config data to a JSON-only API
- Validate YAML structure using JSON schema tools
- Process YAML in JavaScript (which natively parses JSON)
When to Use JSON vs YAML
Use JSON When:
- Building APIs - JSON is the standard for REST APIs
- Browser applications - JavaScript parses JSON natively
- Data interchange - More universal support across languages
- Performance matters - JSON parsers are faster
Use YAML When:
- Writing config files - Docker Compose, Kubernetes, CI/CD pipelines
- Human editing - YAML is easier to read and write by hand
- Need comments - YAML supports inline comments
- Reducing file size - No quotes or brackets needed
Common Conversion Scenarios
Kubernetes Manifests
Kubernetes accepts both JSON and YAML. Most examples use YAML for readability:
# This comment explains the deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3But JSON is valid too and sometimes easier to generate programmatically.
Docker Compose Files
Docker Compose files are YAML by convention. If you have JSON config from an API, convert it to YAML:
version: '3.8'
services:
web:
image: nginx
ports:
- "80:80"CI/CD Pipelines
GitHub Actions, GitLab CI, and CircleCI all use YAML. Convert JSON build configurations to YAML for these platforms.
YAML Features Not in JSON
When converting JSON to YAML, you gain these features:
Comments
# Database configuration
database:
host: localhost # Use 'db' in production
port: 5432Multi-line Strings
description: |
This is a long description
that spans multiple lines
without escape characters.Anchors and Aliases
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
timeout: 60Handling Conversion Edge Cases
Dates and Times
JSON has no date type, so dates are strings. YAML can auto-detect dates:
# YAML interprets this as a date
created: 2024-12-28
# Force it to be a string
created: "2024-12-28"Boolean Values
YAML has multiple boolean representations:
# All of these are true in YAML
enabled: true
enabled: yes
enabled: onWhen converting to JSON, all become true.
Numbers
YAML supports more number formats:
decimal: 1_000_000 # Underscores for readability
octal: 0o755 # File permissions
hex: 0xFF # HexadecimalFrequently Asked Questions
Is the conversion lossless?
JSON to YAML is lossless. YAML to JSON loses comments and may change number/date representations.
Which format should I use for configuration?
Use YAML for human-edited configs. Use JSON for machine-generated configs or API responses.
Can I validate YAML with JSON Schema?
Yes! Convert YAML to JSON first, then validate against your JSON Schema.
Summary
Converting between JSON and YAML is simple with jsoneditor.io. Paste your data, click convert, and get instant results. Use JSON for APIs and data interchange. Use YAML for configuration files where readability and comments matter.
André Figueira