Need to convert JSON to XML in your application? This guide covers everything you need to know about JSON to XML conversion via API, including handling arrays, nested objects, and implementation examples.
Why Convert JSON to XML?
While JSON is the dominant format for modern APIs, many enterprise systems, SOAP services, and legacy applications still require XML. Converting JSON to XML enables integration with these systems while maintaining JSON as your internal format.
Conversion Challenges
JSON to XML conversion has some complexities:
Arrays
JSON arrays don't have a direct XML equivalent. Elements must be wrapped in container tags and given meaningful names.
Root Element
JSON doesn't require a root object, but XML requires a single root element. A wrapper element may need to be added.
Data Types
JSON has distinct types (string, number, boolean). XML represents everything as text, though schemas can enforce types.
Using the JSON to XML API
TinyFn provides a simple endpoint to convert JSON to XML:
POST https://api.tinyfn.io/v1/convert/json-to-xml
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"json": {
"person": {
"name": "John",
"age": 30
}
}
}
{
"xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>John</name><age>30</age></person>"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
json |
object | JSON object to convert (required) |
root_name |
string | Name for root element (default: root) |
array_item_name |
string | Name for array items (default: item) |
pretty |
boolean | Format output with indentation (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch('https://api.tinyfn.io/v1/convert/json-to-xml', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ json: { name: 'John' } })
});
const result = await response.json();
console.log(result.xml); // "<root><name>John</name></root>"
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/convert/json-to-xml',
json={'json': {'name': 'John'}},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['xml']) # "<root><name>John</name></root>"
cURL
curl -X POST "https://api.tinyfn.io/v1/convert/json-to-xml" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"json": {"name": "John"}}'
Common Use Cases
- SOAP Integration: Generate SOAP requests from JSON data
- Legacy Systems: Send data to XML-based enterprise systems
- Document Generation: Create XML documents from JSON templates
- Data Export: Export JSON data in XML format
- API Gateway: Transform responses for XML-expecting clients
Best Practices
- Validate JSON first: Ensure JSON is valid before conversion
- Name elements meaningfully: Use descriptive root and array item names
- Consider namespaces: Add XML namespaces if required by target system
- Test with target system: Verify converted XML works with the destination
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 XML API
Get your free API key and start converting data in seconds.
Get Free API Key