JSON to XML Converter
Paste JSON, get XML instantly. Converts objects, arrays, and @_ attributes in-browser — nothing uploaded. Free, private, no signup required.
Options · 2 spaces · JSON → XML
What is JSON-to-XML Conversion and How Does It Work?
JSON (JavaScript Object Notation) and XML (Extensible Markup Language) are both structured data formats, but they have fundamentally different models: JSON is a tree of objects, arrays, strings, numbers, booleans, and null values with no concept of attributes or document root constraints; XML is a tree of elements that may carry attributes and text content, and the document must have exactly one root element. Converting from JSON to XML requires a set of conventions to bridge this mismatch.
This tool uses the most widely adopted convention — the same one used by fast-xml-parser (Node.js), xmltodict (Python), and JAXB (Java) — applied in reverse:
**1. Root element normalization.** The single most important difference between JSON and XML is the root constraint. JSON has no root concept; XML requires exactly one. The converter handles four cases automatically. A single-key object uses that key as the XML root: { "config": {...} } →
**2. @_ prefix → XML attributes.** JSON keys prefixed with @_ become XML attributes on the enclosing element. { "element": { "@_id": "42", "@_class": "primary" } } produces
**3. #text → element text content.** When an element needs both attributes and text content, the text is stored under the #text key: { "price": { "@_currency": "USD", "#text": "29.99" } } →
**4. Arrays → repeated same-named sibling elements.** XML allows multiple child elements with the same name; JSON uses arrays for ordered lists. A JSON array under a key produces repeated child elements that reuse the key name: { "items": ["a", "b"] } produces
**5. Symmetric with XML-to-JSON.** The @_ and #text conventions used here are exactly the same conventions used by the companion XML to JSON Converter. This means a JSON → XML → JSON round-trip preserves attributes, text content, and element structure — as long as the input JSON follows the @_/#text conventions.
**When to convert JSON to XML?** The most common scenarios are: (1) sending data to a legacy SOAP or XML-based web service that requires an XML request body; (2) generating XML configuration files (Spring, Maven, Ant, Android resources) from JSON data; (3) producing sitemap.xml or RSS feed XML from JSON content data; (4) interoperating with enterprise systems (ERP, CRM, EDI) that consume XML; (5) generating SVG or other XML-based graphics formats programmatically from JSON data. For formatting and validating the resulting XML, use the XML Formatter.
// Convert JSON to XML in Node.js using fast-xml-parser
import { XMLBuilder } from 'fast-xml-parser';
const data = {
catalog: {
product: {
'@_id': 'P01',
'@_category': 'electronics',
name: 'Wireless Headphones',
price: {
'@_currency': 'USD',
'#text': '79.99'
}
}
}
};
const builder = new XMLBuilder({
attributeNamePrefix: '@_', // @_ keys become XML attributes
textNodeName: '#text', // #text key becomes element text content
ignoreAttributes: false, // process @_ attribute keys
format: true, // pretty-print with indentation
indentBy: ' ', // 2-space indent
});
const xml = builder.build(data);
console.log(xml);
// <catalog>
// <product id="P01" category="electronics">
// <name>Wireless Headphones</name>
// <price currency="USD">79.99</price>
// </product>
// </catalog> Key Features
Live Conversion
XML output updates instantly as you type or paste JSON — no Convert button needed. Large inputs (>200KB) automatically switch to manual mode to keep the browser responsive.
Automatic Root Normalization
XML requires exactly one root element. Single-key objects use their key as root; multi-key objects wrap in
@_ Attribute and #text Convention
Keys prefixed with @_ become XML attributes; the #text key becomes element text content. This matches the fast-xml-parser and xmltodict convention, making the output symmetric with the companion XML to JSON Converter.
Array to Repeated Elements
JSON arrays under a key produce repeated child elements that reuse the key name — no singularization. { "items": [1, 2, 3] } yields three
100% Browser-Based Privacy
All conversion runs locally in your browser using JavaScript. Your JSON — including credentials, internal configs, and sensitive payloads — is never sent to any server, never logged, and never stored.
Symmetric Round-Trip with XML to JSON
The @_ and #text conventions are shared with the companion XML to JSON Converter. A JSON → XML → JSON round-trip faithfully preserves attributes, text content, and nested structure.
Examples
API Response Object
{"user":{"id":42,"name":"Alice Kim","email":"alice@example.com","role":"admin","address":{"city":"Seoul","country":"KR"}}} A single-key JSON object where the top-level key becomes the XML root element. The nested address object produces a child
element withTop-Level Array
[{"id":1,"name":"Widget A","price":9.99},{"id":2,"name":"Widget B","price":14.99},{"id":3,"name":"Widget C","price":4.49}] When the entire input is a JSON array (not wrapped in an object), the converter produces
Object with Attributes and Text Content
{"catalog":{"product":{"@_id":"P01","@_category":"electronics","name":"Wireless Headphones","price":{"@_currency":"USD","#text":"79.99"}}}} Keys prefixed with @_ become XML attributes on the element: @_id becomes id="P01" and @_category becomes category="electronics" on the
How to Use
- 1
Paste Your JSON
Enter or paste your JSON data into the input field above. You can also click 'Load example' to try a sample — an API response object, an array payload, or an object using @_ attributes and #text.
- 2
See Live XML Output
XML appears instantly in the output panel. Single-key objects use their key as the root; multi-key objects wrap in
; @_-prefixed keys become attributes; arrays become repeated child elements. Adjust indent (2 or 4 spaces) as needed. - 3
Copy or Download
Click Copy to grab the XML to your clipboard, or Download to save it as a .xml file. To validate or reformat the result, paste it into the XML Formatter.
Common Conversion Pitfalls
Multi-Key Object Gets Unexpected Wrapper
If your top-level JSON has multiple keys, the output wraps them all in
// Input: { "status": 200, "data": { "id": 1 } }
// Output: <root><status>200</status><data><id>1</id></data></root>
// Root is <root> — may not match your schema // Wrap in a named key to control the root element
// Input: { "response": { "status": 200, "data": { "id": 1 } } }
// Output: <response><status>200</status><data><id>1</id></data></response> Top-Level Array Gets - Wrapper
A top-level JSON array has no element name, so it is wrapped as
// Input: [{ "id": 1 }, { "id": 2 }]
// Output: <root><item><id>1</id></item><item><id>2</id></item></root>
// Tag names are generic // Wrap in a named key for meaningful tag names
// Input: { "products": [{ "id": 1 }, { "id": 2 }] }
// Output: <root><products><id>1</id></products><products><id>2</id></products></root>
// Each array element reuses the key name "products" as its tag Attribute Keys Missing @_ Prefix
To produce an XML attribute, the JSON key must be prefixed with @_. Without the prefix, the key is treated as a child element, not an attribute.
// Input: { "tag": { "id": "42", "name": "Alice" } }
// Output: <tag><id>42</id><name>Alice</name></tag> ← id is a child element // Use @_ prefix for attributes
// Input: { "tag": { "@_id": "42", "name": "Alice" } }
// Output: <tag id="42"><name>Alice</name></tag> ← id is an attribute Element with Both Attributes and Text — Missing #text
If you want an element to have both XML attributes and text content, you must use the #text key for the text. Without it, the text has nowhere to go and the element will be empty or produce an error.
// Input: { "price": { "@_currency": "USD", "value": "29.99" } }
// Output: <price currency="USD"><value>29.99</value></price>
// Text is a child element, not text content // Use #text for element text content
// Input: { "price": { "@_currency": "USD", "#text": "29.99" } }
// Output: <price currency="USD">29.99</price> ← text content JSON Keys with XML-Invalid Characters
XML element names cannot start with a digit or contain spaces, colons (except for namespace prefixes), or most special characters. If your JSON keys contain these characters, the output XML will be malformed. Rename offending keys before converting, or sanitize them in a post-processing step.
// JSON key starts with digit — invalid XML element name
// Input: { "1stItem": "value" }
// Output: <1stItem>value</1stItem> ← invalid XML // Use a valid XML element name
// Input: { "firstItem": "value" }
// Output: <firstItem>value</firstItem> ← valid XML Special Characters in Values Not Escaped
XML text content must not contain raw <, >, &, or " characters — they must be escaped as <, >, &, and ". The converter automatically escapes these characters in element text content and attribute values. If downstream processing double-escapes them, verify that your XML parser is consuming raw XML bytes rather than re-escaping already-escaped content.
// JSON: { "query": "name = 'Alice' & role = 'admin'" }
// Converter outputs: <query>name = 'Alice' & role = 'admin'</query>
// If double-processed: &amp; — consumer is re-escaping // Converter output is correct — consume it as XML, not as a raw string // <query>name = 'Alice' & role = 'admin'</query> // XML parser decodes to: name = 'Alice' & role = 'admin'
Common Use Cases
- Calling SOAP and Legacy XML Web Services
- Build the JSON payload in your application, convert it to XML, and POST it to the SOAP endpoint. This avoids hand-building XML strings and lets you work with the data in a native JSON structure up until the moment you need to serialize it for the legacy service.
- Generating XML Configuration Files
- Spring, Maven, Ant, Tomcat, and Android all use XML configuration formats. Generate or transform configuration data as JSON — easier to diff and version-control — then convert to XML for deployment.
- Producing Sitemap and RSS Feed XML
- Content management systems and static site generators often store page metadata as JSON. Convert that metadata to sitemap.xml or RSS feed XML to submit to search engines or syndicate content to feed readers.
- Enterprise Data Integration (EDI / ERP / CRM)
- ERP systems (SAP, Oracle), CRM platforms, and EDI networks commonly exchange data in XML. Convert JSON exports from modern REST APIs to XML to feed these systems without building a custom XML serializer.
- Android Resource Files
- Android string resources, layout constraints, and manifest entries are all XML. Generate or update resource files programmatically from JSON data sources and convert to the required XML format for the Android build system.
- SVG and Vector Graphics Generation
- SVG is XML. Build SVG structure as JSON data — paths, shapes, colors, transforms — then convert to XML to produce valid SVG files for web, print, or vector editing tools.
Technical Details
- RFC 8259 Compliant JSON Parsing
- JSON input is parsed using the browser's native JSON.parse(), which is fully RFC 8259 compliant. It provides accurate syntax error messages with best-effort position information (line and column) and handles all JSON data types: strings, numbers, booleans, null, arrays, and objects.
- Custom Recursive JSON-to-XML Serializer
- After parsing, a custom recursive serializer walks the JSON tree. Object keys that start with @_ are emitted as XML attributes on the parent element. The #text key is emitted as the element's text content. Plain object keys become child elements. Array values produce repeated same-named sibling elements. Special characters (<, >, &, ", ') in text content and attribute values are escaped to their XML entity references.
- Root Normalization
- Because XML requires exactly one root element, the serializer applies root normalization before building the document: single-key objects use the key as root; multi-key objects and top-level arrays receive a synthetic
wrapper. This ensures output is always well-formed XML regardless of input shape. - 100% Browser-Based — No Upload, No Server
- All processing runs in your browser's JavaScript engine. No data is transmitted over the network at any point. Inputs larger than 200KB automatically switch from live mode to manual mode (requiring an explicit Convert click) to keep the browser responsive during heavy serialization.
Best Practices
- Control the Root Element with a Single-Key Object
- The cleanest way to produce meaningful XML is to ensure your top-level JSON is a single-key object whose key is the desired root element name. Wrapping multi-key data under one named key gives you a semantically meaningful root instead of the generic
wrapper. - Use @_ and #text for Attribute-Heavy XML
- If your target XML schema uses many attributes (common in configuration files, SVG, and SOAP envelopes), reshape your JSON to use @_-prefixed keys and #text before converting. The resulting XML will match the target schema exactly without post-processing.
- Wrap Arrays in Named Objects for Meaningful Tag Names
- A top-level array produces the generic
pattern, where- ...
- is a fixed literal name. Wrapping the array under a named key — { "products": [...] } — produces repeated
elements (the key name is reused for every array element) under a wrapper. This gives you a domain-meaningful tag name and is almost always preferable for XML that will be parsed or validated by a schema. - Validate the Output with the XML Formatter
- After converting, paste the XML output into the XML Formatter to validate well-formedness, check indentation, and identify any structural issues before sending the XML to a downstream service or committing it to version control.
- Sanitize JSON Keys Before Converting
- XML element names must start with a letter or underscore and may not contain spaces or most special characters. If your JSON keys come from an external source (a database column name, an API field name, or user input), audit them for XML name validity before converting to avoid malformed output.
Frequently Asked Questions
Is my JSON data sent to a server when I use this tool?
How does the tool decide what the XML root element is?
Why does a multi-key JSON object get wrapped in ?
How does a top-level JSON array convert to XML?
How do I convert JSON keys to XML attributes?
What is the #text key used for?
Does indentation affect the XML structure?
How does a JSON array nested inside an object convert to XML?
How do I convert XML back to JSON?
Can I validate or format the XML output?
Is there a file size limit for JSON input?
What JSON types are supported?
Related Tools
View all tools →Base64 Decoder & Encoder
Encoding & Formatting
Decode and encode Base64 online for free. Real-time conversion with full UTF-8 and emoji support. 100% private — runs in your browser. No signup needed.
CSV to JSON Converter
Encoding & Formatting
Convert CSV to JSON in your browser. RFC 4180, type inference, header row, big-int safe. 100% private, no upload.
JSON Diff & Compare
Encoding & Formatting
Compare two JSON files instantly in your browser. Side-by-side highlighting, RFC 6902 JSON Patch output, ignore noisy fields like timestamps and IDs. 100% private, no upload.
JSON Formatter & Validator
Encoding & Formatting
Format, validate and beautify JSON instantly in your browser. Free online tool with syntax validation, error detection, minify and one-click copy. 100% private.
JSON Schema Validator
Encoding & Formatting
Validate JSON against any JSON Schema instantly in your browser. Supports Draft 2020-12, 2019-09, and Draft-07 with path-precise error messages. 100% private — no upload, no account, free.
JSON to CSV Converter
Encoding & Formatting
Convert JSON to CSV in your browser. RFC 4180, Excel-EU, TSV, Pipe presets. Flatten nested or stringify. 100% private, no upload.