Need to convert YAML to JSON in your application? This guide covers everything you need to know about YAML to JSON conversion via API, including YAML features, handling multi-document files, and implementation examples.
Why Convert YAML to JSON?
YAML is popular for configuration files due to its human readability, but many APIs and applications expect JSON. Converting YAML to JSON enables using YAML for human-friendly configs while maintaining JSON compatibility for your applications.
YAML Features
YAML has several features to consider during conversion:
Comments
YAML supports comments (# comment), which are lost during JSON conversion since JSON doesn't support comments.
Multi-line Strings
YAML has multiple ways to represent multi-line strings (| and >) which convert to regular JSON strings.
Anchors and Aliases
YAML supports references (&anchor and *alias) which are expanded during conversion.
Using the YAML to JSON API
TinyFn provides a simple endpoint to convert YAML to JSON:
POST https://api.tinyfn.io/v1/convert/yaml-to-json
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"yaml": "name: John\nage: 30\ncities:\n - New York\n - Boston"
}
{
"json": {
"name": "John",
"age": 30,
"cities": ["New York", "Boston"]
}
}
Parameters
| Parameter | Type | Description |
|---|---|---|
yaml |
string | YAML string to convert (required) |
multi_document |
boolean | Parse multiple YAML documents (default: false) |
Code Examples
JavaScript / Node.js
const yaml = `name: John
age: 30
active: true`;
const response = await fetch('https://api.tinyfn.io/v1/convert/yaml-to-json', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ yaml })
});
const result = await response.json();
console.log(result.json); // {name: "John", age: 30, active: true}
Python
import requests
yaml_data = """name: John
age: 30
active: true"""
response = requests.post(
'https://api.tinyfn.io/v1/convert/yaml-to-json',
json={'yaml': yaml_data},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['json']) # {"name": "John", "age": 30, "active": true}
cURL
curl -X POST "https://api.tinyfn.io/v1/convert/yaml-to-json" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"yaml": "name: John\nage: 30"}'
Common Use Cases
- Config Processing: Parse YAML config files for applications
- CI/CD Pipelines: Process YAML pipeline definitions
- Kubernetes: Convert K8s manifests for programmatic access
- API Integration: Transform YAML data for JSON APIs
- Data Migration: Convert YAML databases to JSON
Best Practices
- Validate YAML syntax: Ensure YAML is valid before conversion
- Handle multi-document: Decide how to handle --- document separators
- Preserve types: YAML auto-detects types - verify conversions
- Document comments: Comments are lost - consider preserving them separately
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 YAML to JSON API
Get your free API key and start converting data in seconds.
Get Free API Key