XML to JSON API: The Complete Guide

Need to convert XML to JSON in your application? This guide covers everything you need to know about XML to JSON conversion via API, including handling attributes, arrays, and implementation examples.

Why Convert XML to JSON?

While XML and JSON both represent structured data, JSON has become the preferred format for web APIs due to its simplicity, smaller size, and native JavaScript support. Converting XML to JSON enables easier data handling in modern applications.

Conversion Challenges

XML to JSON conversion has some complexities:

Attributes vs Elements

XML has both attributes and child elements. JSON only has properties. Attributes are typically prefixed with @ or _ in the resulting JSON.

Arrays

XML doesn't distinguish between single elements and arrays. Conversion rules must determine when to create arrays.

Text Content

Mixed content (text with child elements) requires special handling, often using a #text property.

Note: Our API uses conventions like @ for attributes and auto-detects arrays for consistent conversion results.

Using the XML to JSON API

TinyFn provides a simple endpoint to convert XML to JSON:

API Request
POST https://api.tinyfn.io/v1/convert/xml-to-json
Headers: X-API-Key: your-api-key
Content-Type: application/json

{
  "xml": "<person id=\"1\"><name>John</name><age>30</age></person>"
}
Response
{
  "json": {
    "person": {
      "@id": "1",
      "name": "John",
      "age": "30"
    }
  }
}

Parameters

Parameter Type Description
xml string XML string to convert (required)
attribute_prefix string Prefix for attributes (default: @)
always_array boolean Force all elements to arrays (default: false)

Code Examples

JavaScript / Node.js

const response = await fetch('https://api.tinyfn.io/v1/convert/xml-to-json', {
  method: 'POST',
  headers: {
    'X-API-Key': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ xml: '<name>John</name>' })
});
const result = await response.json();
console.log(result.json); // { name: "John" }

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/convert/xml-to-json',
    json={'xml': '<name>John</name>'},
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['json'])  # {"name": "John"}

cURL

curl -X POST "https://api.tinyfn.io/v1/convert/xml-to-json" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"xml": "<name>John</name>"}'

Common Use Cases

  • API Migration: Convert legacy XML APIs to JSON
  • Data Integration: Transform XML feeds for JSON-based systems
  • SOAP to REST: Convert SOAP responses for REST clients
  • Config Files: Transform XML configs to JSON
  • Data Pipeline: Process XML data in JSON-native tools

Best Practices

  1. Validate XML first: Ensure XML is well-formed before conversion
  2. Handle encoding: Specify character encoding in XML declaration
  3. Test edge cases: Verify handling of attributes, arrays, and mixed content
  4. Document conventions: Document how attributes and arrays are handled

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 XML to JSON API

Get your free API key and start converting data in seconds.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key