Need to convert JSON to YAML in your application? This guide covers everything you need to know about JSON to YAML conversion via API, including formatting options and implementation examples.
Why Convert JSON to YAML?
YAML is more human-readable than JSON, making it ideal for configuration files, documentation, and data that humans need to read and edit. Converting JSON to YAML creates cleaner, more maintainable configuration files.
Benefits of YAML
YAML offers several advantages over JSON:
Readability
No curly braces or quotes for strings (usually). Uses indentation for structure.
Comments
YAML supports comments (# comment) for documentation, which JSON lacks.
Multi-line Strings
YAML has elegant syntax for multi-line strings using | or > characters.
Using the JSON to YAML API
TinyFn provides a simple endpoint to convert JSON to YAML:
POST https://api.tinyfn.io/v1/convert/json-to-yaml
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"json": {
"name": "John",
"age": 30,
"cities": ["New York", "Boston"]
}
}
{
"yaml": "name: John\nage: 30\ncities:\n - New York\n - Boston\n"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
json |
object | JSON object to convert (required) |
indent |
integer | Indentation spaces (default: 2) |
flow_style |
boolean | Use flow style for small collections (default: false) |
Code Examples
JavaScript / Node.js
const json = {
name: "John",
age: 30,
active: true
};
const response = await fetch('https://api.tinyfn.io/v1/convert/json-to-yaml', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ json })
});
const result = await response.json();
console.log(result.yaml); // "name: John\nage: 30\nactive: true\n"
Python
import requests
json_data = {
"name": "John",
"age": 30,
"active": True
}
response = requests.post(
'https://api.tinyfn.io/v1/convert/json-to-yaml',
json={'json': json_data},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['yaml']) # "name: John\nage: 30\nactive: true\n"
cURL
curl -X POST "https://api.tinyfn.io/v1/convert/json-to-yaml" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"json": {"name": "John", "age": 30}}'
Common Use Cases
- Config Generation: Generate YAML configs from JSON templates
- Documentation: Create human-readable data representations
- Kubernetes: Generate K8s manifests from JSON
- CI/CD: Create pipeline configurations
- Docker Compose: Generate compose files programmatically
Best Practices
- Choose consistent style: Use block style for readability, flow for compactness
- Set indentation: 2 spaces is common, but some prefer 4
- Add comments separately: Comments must be added after conversion
- Validate output: Ensure generated YAML parses correctly
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-convert": {
"url": "https://api.tinyfn.io/mcp/convert/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all conversion tools available via MCP in our Conversion MCP Tools for AI Agents guide.
Try the JSON to YAML API
Get your free API key and start converting data in seconds.
Get Free API Key