How to Convert JSON to XML Without Losing Attributes or Structure
XML has no native concept of arrays or attributes the way JSON does. This guide explains the mapping rules and shows how to convert JSON to clean, valid XML without uploading anything.
JSON and XML both describe structured data, but they do not think about it the same way. JSON has objects, arrays, and primitive values. XML has elements, attributes, and text nodes - and no built-in idea of a list at all. Converting between the two means making a set of deliberate choices about which JSON keys become attributes, which become child elements, and how a repeated array turns into repeated tags.
This guide walks through those choices and the escaping rules that keep the output well-formed, then shows how to apply them instantly with the JSON to XML Converter - a browser-based tool that turns any JSON object into indented, valid XML without ever sending your data to a server.
Why convert JSON to XML at all?
Most new APIs speak JSON by default, but plenty of systems still expect XML: SOAP and XML-RPC web services, enterprise middleware, RSS and Atom feeds, and configuration formats inherited from older platforms. If your application's source of truth is JSON - a database export, a REST response, a config object in code - you often need an XML representation to hand to one of these systems without rewriting your data model twice.
Rather than hand-building XML strings with string concatenation (a reliable source of malformed markup and unescaped ampersands), a dedicated converter walks the JSON tree and emits correctly nested, correctly escaped XML every time.
The core mapping problem: elements, attributes and arrays
JSON objects map naturally to XML elements: a key becomes a tag name, and its value becomes that tag's content. The hard part is that XML elements can carry two different kinds of information - attributes on the opening tag, and child content inside it - while a plain JSON key-value pair can't express that distinction on its own.
The JSON to XML Converter solves this with a simple, explicit convention: any key prefixed with @ becomes an XML attribute on its parent element, and everything else becomes a child element. A special #text key supplies inner text content when an element also needs attributes.
- A plain key like
"title": "..."becomes a child element<title>...</title>. - An
@-prefixed key like"@id": "b1"becomes an attribute:id="b1"on the enclosing tag. - A
#textkey supplies the element's own text when it also has attributes. - An array value repeats the same tag name once per item, as sibling elements - XML's answer to a JSON list.
Attributes vs. elements: a worked example
Consider a book record with an identifier, a title, and an author. If you want the identifier to render as an XML attribute rather than a nested tag, you prefix that key with @ before converting.
Object with an attribute
The @id key becomes an attribute; title and author become child elements.
Input
{
"book": {
"@id": "b1",
"title": "The Pragmatic Programmer",
"author": "Hunt & Thomas"
}
}Output
<?xml version="1.0" encoding="UTF-8"?>
<book id="b1">
<title>The Pragmatic Programmer</title>
<author>Hunt & Thomas</author>
</book>Notice two things in that output: the @id key disappears from the element body and reappears as an attribute on <book>, and the ampersand in "Hunt & Thomas" is automatically escaped to & so the document still parses correctly.
Mixed content: attributes and text together
Sometimes an element needs both an attribute and inline text - a price with a currency code, or a measurement with a unit. That's exactly what the #text key is for: it supplies the text node that sits alongside any @-prefixed attributes on the same object.
{
"price": {
"@currency": "USD",
"#text": "9.99"
}
}That JSON produces <price currency="USD">9.99</price> - a single element carrying both a structured attribute and readable text, which is a very common shape in real-world XML like invoices and product feeds.
How arrays become repeated elements
XML has no array type. A JSON array under a key like "items" is expanded into one <items> element per entry, written as siblings in document order - not wrapped in an extra container unless you add one yourself. This matches how most XML formats represent lists: repeated tags with the same name, one after another.
That also means round-tripping isn't always lossless by default: three orders in an array and three orders as separate keys can look identical once serialized. If you plan to convert the result back with XML to JSON, keep arrays under a single distinct key name so the structure comes back the way you expect.
Step-by-step: convert JSON to XML
- Open the JSON to XML Converter and paste your JSON object into the input editor.
- Prefix any key that should become an XML attribute with
@, and use#textfor inner text on elements that also need attributes. - Convert - the tool walks the JSON tree, builds nested elements, expands arrays into repeated tags, and escapes special characters as it serializes.
- Review the indented XML output, including the optional XML declaration and your chosen root element name.
- Copy the result or download it as an
.xmlfile for your SOAP call, feed, or config.
Common use cases for JSON to XML conversion
- Sending JSON application data to a legacy SOAP or XML-RPC web service that only accepts XML requests.
- Generating an RSS or Atom feed from a JSON array of articles or products.
- Producing XML configuration or manifest files from a JSON source of truth used elsewhere in a codebase.
- Bridging a modern JSON REST API with an XML-only enterprise system during a migration.
- Building XML test fixtures to exercise an XML parser or schema validator.
Common mistakes and how to avoid them
Forgetting the @ prefix
If you leave a key without the @ prefix, it becomes a child element instead of an attribute - so "id": "b1" produces <id>b1</id> rather than id="b1" on the parent tag. If the receiving system expects an attribute, check your key names before converting.
Assuming special characters don't need escaping
Ampersands, angle brackets, and quotes are not valid as raw text inside XML - &, <, >, ", and ' must be escaped to &, <, >, ", and '. Hand-written XML often skips this and breaks the first time a value contains one of these characters. A converter handles it automatically, every time.
Converting a top-level array without a wrapping key
XML documents need exactly one root element. A bare JSON array at the top level has nowhere to attach a root tag, so wrap it in an object first - for example { "items": [ ... ] } - and choose a meaningful root element name for the converter to use.
Expecting XML namespaces for free
A general-purpose JSON-to-XML mapping does not infer namespace prefixes or schema-specific rules on its own. If the target system requires a namespaced XML dialect, add the namespace keys explicitly in your JSON (as attributes, using the @ convention) before converting.
Best practices for reliable JSON-to-XML output
- Decide up front which fields are attributes vs. elements, and apply the
@convention consistently across your JSON so the resulting XML has a predictable shape. - Keep arrays under distinct, singular-plus-plural key pairs (e.g. an
"order"array under"orders") so the structure is unambiguous if you ever convert it back with XML to JSON. - Validate your source JSON first with a JSON Validator - a converter can only produce correct XML from correct JSON.
- If the target system is XML-only for historical reasons, keep your actual source of truth in JSON and generate XML at the boundary, rather than maintaining both formats by hand.
- For payloads containing credentials, internal identifiers or customer data, use a converter that runs entirely client-side rather than pasting sensitive JSON into a server-side web tool.
Your JSON never leaves your browser
The JSON to XML Converter parses, transforms, and serializes your data entirely client-side. Nothing is uploaded, logged, or stored - so it's safe to convert payloads containing API tokens, customer records, or internal system identifiers.
Working the other direction?
If you need to bring XML back into JSON - say, a SOAP response you want to consume in a JavaScript app - the companion XML to JSON Converter follows the same @ and #text conventions in reverse.
Related tools
Parse XML into predictable JSON with clear attribute and text mapping.
Turn JSON into clean, readable YAML for configs, pipelines and manifests.
Flatten an array of JSON objects into spreadsheet-ready CSV.
Pretty-print JSON with configurable indentation and instant validation.
Related guides
XML and JSON model data differently, so a naive conversion loses attributes or flattens structure. This guide explains a predictable XML-to-JSON convention and how to apply it safely.
Kubernetes, GitHub Actions and Ansible all expect YAML, but your data often starts life as JSON. Here's how the conversion works, where it trips people up, and how to do it privately in seconds.
Spreadsheets don't understand nested JSON. This guide explains how a JSON array becomes clean, aligned CSV rows, and how to avoid the mistakes that corrupt spreadsheet imports.
Minified JSON is unreadable and hard to debug. This guide covers how JSON formatting works, when to use it, common mistakes, and how to pretty-print JSON without uploading it anywhere.