JSON to XML
Convert JSON to XML format
JSON Input
XML Output
What is JSON to XML Conversion?
JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are the two dominant data interchange formats in software development. JSON uses key-value pairs and arrays in a compact syntax, while XML wraps data in opening and closing tags with optional attributes. Converting JSON to XML means translating JSON's structural primitives — objects, arrays, strings, numbers, booleans, and null — into a well-formed XML document with matching element nesting.
XML remains the required format in many enterprise systems, government APIs, SOAP web services, and industry standards like HL7 (healthcare), FpML (finance), and XBRL (financial reporting). When your application produces JSON but the downstream consumer expects XML, a JSON to XML converter handles the translation without manual restructuring. The conversion follows predictable mapping rules: JSON objects become XML elements, array items become repeated sibling elements, and primitive values become text nodes.
There is no single RFC or W3C standard that defines how JSON maps to XML. Different libraries produce different output for the same input. The most common convention (used by this tool) wraps the entire document in a configurable root element, converts each JSON key to an XML child element, and represents array items as repeated elements with the same tag name. Understanding these mapping rules matters when the receiving system enforces a specific XML schema (XSD).
Why Use an Online JSON to XML Converter?
Writing XML by hand from a JSON source is tedious and error-prone. Mismatched tags, forgotten closing elements, and incorrect nesting produce documents that fail schema validation. A converter handles the structural translation automatically.
JSON to XML Use Cases
JSON to XML Mapping Rules
Because no universal standard governs JSON-to-XML conversion, different tools produce different output. The table below shows the mapping conventions used by this converter and most popular libraries (js2xmlparser, xmlbuilder, fast-xml-parser). The repeated-sibling convention for arrays became dominant because it mirrors how XML schemas naturally model collections — each item is a first-class element rather than a wrapped child — and it integrates cleanly with XPath queries and XSLT transforms.
| JSON Type | JSON Example | XML Output |
|---|---|---|
| Object | {"name": "Alice"} | <name>Alice</name> |
| Nested object | {"user": {"age": 30}} | <user><age>30</age></user> |
| Array | {"colors": ["red", "blue"]} | <colors>red</colors><colors>blue</colors> |
| String | "hello" | <root>hello</root> |
| Number | 42 | <root>42</root> |
| Boolean | true | <root>true</root> |
| Null | null | <root/> |
| Empty object | {} | <root/> |
| Empty array | [] | (no child elements) |
Code Examples
Below are runnable snippets for converting JSON to XML in three environments. Each example produces well-formed XML from a sample JSON object.
import { create } from 'xmlbuilder2';
const json = {
order: {
id: 1024,
items: [
{ sku: "A1", qty: 2 },
{ sku: "B3", qty: 1 }
],
shipped: false
}
};
const xml = create({ version: '1.0' })
.ele(json)
.end({ prettyPrint: true });
console.log(xml);
// → <?xml version="1.0"?>
// → <order>
// → <id>1024</id>
// → <items>
// → <sku>A1</sku>
// → <qty>2</qty>
// → </items>
// → <items>
// → <sku>B3</sku>
// → <qty>1</qty>
// → </items>
// → <shipped>false</shipped>
// → </order>import json
import xmltodict
data = {
"order": {
"id": 1024,
"items": [
{"sku": "A1", "qty": 2},
{"sku": "B3", "qty": 1}
],
"shipped": False
}
}
# xmltodict.unparse expects a single root key
xml = xmltodict.unparse(data, pretty=True)
print(xml)
# → <?xml version="1.0" encoding="utf-8"?>
# → <order>
# → <id>1024</id>
# → <items>
# → <sku>A1</sku>
# → <qty>2</qty>
# → </items>
# → <items>
# → <sku>B3</sku>
# → <qty>1</qty>
# → </items>
# → <shipped>false</shipped>
# → </order>package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type Item struct {
SKU string `json:"sku" xml:"sku"`
Qty int `json:"qty" xml:"qty"`
}
type Order struct {
XMLName xml.Name `xml:"order"`
ID int `json:"id" xml:"id"`
Items []Item `json:"items" xml:"items"`
Shipped bool `json:"shipped" xml:"shipped"`
}
func main() {
raw := `{"id":1024,"items":[{"sku":"A1","qty":2},{"sku":"B3","qty":1}],"shipped":false}`
var order Order
json.Unmarshal([]byte(raw), &order)
out, _ := xml.MarshalIndent(order, "", " ")
fmt.Println(xml.Header + string(out))
// → <?xml version="1.0" encoding="UTF-8"?>
// → <order>
// → <id>1024</id>
// → <items>
// → <sku>A1</sku>
// → <qty>2</qty>
// → </items>
// → ...
}