How to Convert YAML to JSON: Every Method Covered
YAML and JSON solve the same problem — representing structured data — but for different audiences. YAML is optimized for humans writing configuration. JSON is optimized for machines exchanging data. When you need to bridge the two, this guide covers every conversion method from a quick browser-based tool to production-ready scripts.
What Is YAML and When Is JSON Needed Instead?
YAML is the default configuration language for Kubernetes, Docker Compose, GitHub Actions, CircleCI, Travis CI, Ansible, Helm, and most modern DevOps tooling. Its indentation-based hierarchy makes nested configurations readable without brackets or quotes cluttering every line.
JSON is the standard data exchange format for REST APIs, GraphQL payloads, NoSQL databases like MongoDB, configuration in package.json and tsconfig.json, and most cloud provider API calls including AWS, Azure, and Google Cloud. When YAML data needs to travel through any of these systems, converting to JSON is necessary.
Is YAML a superset of JSON?
Yes — technically, JSON is a valid subset of YAML 1.2. Every valid JSON document can be parsed by a YAML parser without modification. However, YAML has features JSON does not — comments, anchors and aliases, multi-line strings, and more expressive boolean handling. When converting from YAML to JSON, these YAML-only features are either converted (anchors become inline values) or dropped (comments are removed).
Convert YAML to JSON Online — Free Tool
The quickest way to convert YAML to JSON is Format Pilot’s free online converter. It runs entirely in your browser — paste YAML, get JSON, copy and go. Your data is never uploaded to any server. It handles Kubernetes manifests, Docker Compose files, GitHub Actions workflows, and any other standard YAML document.
Practical Conversion Examples
Docker Compose YAML to JSON
# YAML
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
db:
image: postgres:14
environment:
POSTGRES_DB: myapp
// JSON output
{
"version": "3.8",
"services": {
"web": {
"image": "nginx:alpine",
"ports": ["80:80"]
},
"db": {
"image": "postgres:14",
"environment": {
"POSTGRES_DB": "myapp"
}
}
}
}
Convert YAML to JSON in the Browser with JavaScript
// Using js-yaml in browser via CDN
import * as yaml from 'https://cdn.jsdelivr.net/npm/js-yaml@4/+esm';
function convertYamlToJson(yamlString) {
const parsed = yaml.load(yamlString);
return JSON.stringify(parsed, null, 2);
}
const yamlInput = document.getElementById('yaml-input').value;
const jsonOutput = convertYamlToJson(yamlInput);
console.log(jsonOutput);
Automating YAML to JSON Conversion in a CI/CD Pipeline
In GitHub Actions, you can automate YAML-to-JSON conversion as part of a workflow step. This is useful when deploying configuration from YAML source files to systems that require JSON input:
- name: Convert config to JSON
run: |
python3 -c "
import yaml, json
with open('config.yaml') as f:
data = yaml.safe_load(f)
with open('config.json', 'w') as f:
json.dump(data, f, indent=2)
"
Frequently Asked Questions
Does converting YAML to JSON lose any data?
Comments are dropped since JSON has no comment syntax. YAML anchors and aliases are resolved and inlined. All actual data values — strings, numbers, booleans, nulls, arrays, and objects — are preserved exactly. The conversion is lossless for data.
How do I handle YAML files with multiple documents?
Multi-document YAML files use --- as a separator. In Python, use yaml.safe_load_all() to iterate over all documents and convert each one to a separate JSON object. For single-document conversion, the standard yaml.safe_load() is sufficient.
Can I convert JSON back to YAML after converting YAML to JSON?
Yes — JSON to YAML conversion is fully reversible for data. Use Format Pilot’s converter and switch the direction to JSON → YAML. The structure and all values will be preserved. Note that original YAML comments and anchor definitions will not be restored — those were lost during the initial YAML-to-JSON conversion.