Why Convert Between YAML and JSON?
In microservice architectures and DevOps practices, we often encounter scenarios like:
- Configuration management (YAML is more human-readable)
- API data interchange (JSON is the web standard format)
- Multi-cloud deployment (different platforms have different format requirements)
- Data persistence (JSON is easier to compress and store)
Efficient YAML to JSON Conversion with Python
1.1 Preparation
# conda environment
pip install pyyaml
Complete YAML to JSON Conversion Process
import yaml
import json
import sys
import os
def yaml_to_json(yaml_str):
try:
data = yaml.safe_load(yaml_str)
return json.dumps(data, indent=2, ensure_ascii=False, default=str)
except yaml.YAMLError as e:
print(f"YAML parsing error: {e.problem} {e.context}")
except Exception as e:
print(f"Unknown error: {str(e)}")
if __name__ == "__main__":
if len(sys.argv) > 1:
yaml_file = sys.argv[1]
if not os.path.isfile(yaml_file):
print(f"File not found: {yaml_file}")
sys.exit(1)
with open(yaml_file, 'r', encoding='utf-8') as f:
yaml_content = f.read()
json_output = yaml_to_json(yaml_content)
json_file = os.path.splitext(yaml_file)[0] + ".json"
with open(json_file, 'w', encoding='utf-8') as f:
f.write(json_output)
print(f"Successfully converted YAML to JSON, output file: {json_file}")
else:
# Default test data
yaml_content = """
param:
Author: Yanzhi
Email: yanzhi@bobobk.com
Sample: RNA
Number: 12
Source: Lab
data:
indexdir: /index/
filedir: /rawdata
filenames:
- MERS_set2_0h_1
- MERS_set2_0h_2
- MERS_set2_16h_1
- MERS_set2_16h_2
- MERS_set2_48h_1
- MERS_set2_48h_2
- SARS_0h_1
- SARS_0h_2
- SARS_16h_1
- SARS_16h_2
- SARS_48h_1
- SARS_48h_2
"""
print(yaml_to_json(yaml_content))
{
"param": {
"Author": "Yanzhi",
"Email": "yanzhi@bobobk.com",
"Sample": "RNA",
"Number": 12,
"Source": "Lab"
},
"data": {
"indexdir": "/index/",
"filedir": "/rawdata",
"filenames": [
"MERS_set2_0h_1",
"MERS_set2_0h_2",
"MERS_set2_16h_1",
"MERS_set2_16h_2",
"MERS_set2_48h_1",
"MERS_set2_48h_2",
"SARS_0h_1",
"SARS_0h_2",
"SARS_16h_1",
"SARS_16h_2",
"SARS_48h_1",
"SARS_48h_2"
]
}
}
Key Feature Explanation:
python yaml_to_json.py config.yaml
Advanced JSON to YAML Usage
import json
import yaml
import sys
import os
def json_to_yaml(json_str):
try:
data = json.loads(json_str)
return yaml.dump(data, allow_unicode=True, sort_keys=False)
except json.JSONDecodeError as e:
print(f"JSON parsing error: {e.msg} at line {e.lineno} column {e.colno}")
except Exception as e:
print(f"Unknown error: {str(e)}")
if __name__ == "__main__":
if len(sys.argv) > 1:
json_file = sys.argv[1]
if not os.path.isfile(json_file):
print(f"File not found: {json_file}")
sys.exit(1)
with open(json_file, 'r', encoding='utf-8') as f:
json_content = f.read()
yaml_output = json_to_yaml(json_content)
yaml_file = os.path.splitext(json_file)[0] + ".yaml"
with open(yaml_file, 'w', encoding='utf-8') as f:
f.write(yaml_output)
print(f"Successfully converted JSON to YAML, output file: {yaml_file}")
else:
# Test JSON data (consistent structure with previous YAML test data)
json_content = """
{
"param": {
"Author": "Yanzhi",
"Email": "yanzhi@bobobk.com",
"Sample": "RNA",
"Number": 12,
"Source": "Lab"
},
"data": {
"indexdir": "/index/",
"filedir": "/rawdata",
"filenames": [
"MERS_set2_0h_1",
"MERS_set2_0h_2",
"MERS_set2_16h_1",
"MERS_set2_16h_2",
"MERS_set2_48h_1",
"MERS_set2_48h_2",
"SARS_0h_1",
"SARS_0h_2",
"SARS_16h_1",
"SARS_16h_2",
"SARS_48h_1",
"SARS_48h_2"
]
}
}
"""
print(json_to_yaml(json_content))
param:
Author: Yanzhi
Email: yanzhi@bobobk.com
Sample: RNA
Number: 12
Source: Lab
data:
indexdir: /index/
filedir: /rawdata
filenames:
- MERS_set2_0h_1
- MERS_set2_0h_2
- MERS_set2_16h_1
- MERS_set2_16h_2
- MERS_set2_48h_1
- MERS_set2_48h_2
- SARS_0h_1
- SARS_0h_2
- SARS_16h_1
- SARS_16h_2
- SARS_48h_1
- SARS_48h_2
JavaScript Browser-Side Solution
Browser-side implementation
JSON to YAML
<script>
function convertToYaml() {
const input = document.getElementById("jsonInput").value;
const output = document.getElementById("yamlOutput");
try {
const obj = JSON.parse(input);
const yaml = jsyaml.dump(obj, { sortKeys: false });
output.textContent = yaml;
} catch (e) {
output.textContent = "❌ JSON parsing error: " + e.message;
}
}
</script>
YAML to JSON
function convertToJson() {
const yamlText = document.getElementById("yamlInput").value;
const output = document.getElementById("outputArea");
try {
const obj = jsyaml.load(yamlText);
const json = JSON.stringify(obj, null, 2);
output.textContent = json;
} catch (e) {
output.textContent = "❌ YAML parsing error: " + e.message;
}
}
Online Version
Generate HTML based on the JavaScript version: Online YAML JSON converter
Core Differences and Best Practices
Format Comparison
Feature | YAML | JSON |
---|---|---|
Comments | Supports # comments |
Not supported |
Data Types | Extended types (date, regex) | Basic types |
Readability | High | Medium |
File Size | Smaller | Larger |
- Needs human editing? → Choose YAML
- Needs strict Schema? → Choose JSON
- Needs cross-platform transfer? → Choose JSON
- Needs version control? → Choose YAML (differences are clearer)
Common Problem Solutions
Special character escaping: JSON requires escaping double quotes, while YAML supports a mix of single and double quotes.
Date format handling:
# Python custom converter
class SafeDateYamlLoader(yaml.SafeLoader):
pass
def timestamp_constructor(loader, node):
return datetime.fromisoformat(loader.construct_scalar(node))
SafeDateYamlLoader.add_constructor('!datetime', timestamp_constructor)
Large file processing techniques: Read and convert in chunks.
Conclusion
Through this detailed guide, you should now have a solid understanding of:
- The core methods for format conversion in both languages.
- The best practices for different scenarios.
- Strategies for handling common issues.
Practical Application Suggestions:
- Prioritize YAML for configuration management.
- Recommend JSON format for API interactions.
- Pay attention to compatibility for complex data types.
Mastering format conversion skills will significantly boost your development efficiency. It’s recommended to combine this with getting a ChatGPT API key for automated script development and building intelligent data processing workflows.