My Tool Studio
Developer

JSON to XML Converter

This JSON to XML converter turns a JSON object or array into well-formed XML and updates as you type. Nested objects become nested elements and array items repeat their element name, so the structure carries over. Keys that start with a prefix you choose, such as @ or _, become attributes, and a #text key sets an element's text. Set the root element and array item names, pick 2 or 4 spaces, tabs or minified output, and decide whether to include the XML declaration and self-close empty elements. Keys that are not legal XML names are renamed so the output stays valid. Paste JSON, drop or upload a file, or load it from a URL, then copy or download the .xml file.

Always freeNo sign upRuns in your browser
Swap: XML to JSON

If the JSON is an object with one top-level key, that key is the root. Otherwise the output is wrapped in the root element, and top-level array entries use the item element. A "#text" key becomes the element's text.

XML appears here.

How to use

01

Add JSON

Paste JSON into the JSON box, drop or upload a .json file, use Load URL, or press Try sample to load a small book catalog that uses @ attributes and an array.

02

Set names and options

Type a Root element and Array item element name, pick the Indent, and choose an attribute prefix. Tick or untick XML declaration and Self-close empty elements. The XML updates as you type.

03

Copy or download the XML

Press Copy above the XML pane, or Download .xml to save the file. Swap: XML to JSON opens the reverse converter.

Why JSON to XML Converter

JSON to XML mapping rules

The converter follows a small set of fixed rules. Knowing them makes it easy to shape your JSON so the XML comes out the way a receiving system expects.

The examples assume the default names, root for the root element and item for top-level array entries, with the @ attribute prefix and self-closing empty elements. The rules apply at every level of nesting.

JSONXML output
One top-level key: {"book": {...}}<book>...</book> becomes the root
Several top-level keys: {"a": 1, "b": 2}<root><a>1</a><b>2</b></root>
Nested object: {"price": {"amount": 9}}<price><amount>9</amount></price>
Array value: {"tag": ["a", "b"]}<tag>a</tag><tag>b</tag>
Top-level array: [{"id": 1}]<root><item><id>1</id></item></root>
Attribute key: {"@id": "bk1"}id="bk1" on the parent element
Text key: {"@lang": "en", "#text": "Dune"}lang="en" on the element, with Dune as its text
Null: {"notes": null}<notes/>
Invalid name: {"first name": "Ada"}<first_name>Ada</first_name>

What does not carry over from JSON to XML

JSON and XML describe data in different ways, so a few details change on the way across. Check these before you send the XML to a strict receiver, such as a SOAP service with a schema.

  • Types: XML text is untyped, so numbers, booleans and strings all become text. A schema or the receiving code decides how to read them.
  • Empty arrays produce no element at all, so the receiver cannot tell an empty list from a missing one.
  • Attributes versus elements: only keys that start with the chosen attribute prefix become attributes. Everything else is an element, even if the receiving format expects an attribute.
  • Namespaces: prefixes such as soap:Envelope are kept as part of the element name, but you have to add the xmlns attributes yourself, for example as an @xmlns:soap key.
  • Order: elements follow the key order of your JSON, which matters when a schema requires a fixed sequence.

Common questions

How are JSON arrays converted to XML?
Each item repeats the element named by its key, so {"tag": ["a", "b"]} becomes <tag>a</tag><tag>b</tag>. When the whole JSON is an array, every item is wrapped in the Array item element inside the root element.
Can I set the root element name when converting JSON to XML?
Yes, type it in the Root element field. It is used when the JSON has several top-level keys, is an array or is a plain value. If the JSON is an object with exactly one key whose value is not an array, that key becomes the root and the field is not needed.
How do I make JSON keys become XML attributes?
Start the key with the attribute prefix, @ by default, and keep Keys with a prefix become attributes ticked. {"book": {"@id": "bk101", "title": "Dune"}} becomes <book id="bk101"><title>Dune</title></book>. If your JSON uses _id or -id for attributes, pick that prefix instead.
Is the conversion from JSON to XML lossless?
Not completely. XML text has no data types, so 1, "1" and true all become plain text, and null becomes an empty element. Keys that had to be renamed also change. The structure and values are kept, but a tool reading the XML back has to guess which values were numbers.
How do I convert XML back to JSON?
Use the XML to JSON Converter. Set its attribute prefix to @ so attributes come back as @ keys, and the result converts back to XML here without renaming anything.
What happens to JSON keys that are not valid XML names?
Characters other than letters, digits, underscores, hyphens, periods and colons are replaced with underscores, and a name that starts with a digit, hyphen or period gets a leading underscore. So "first name" becomes first_name and "2024" becomes _2024.
How are null, true and false written in the XML output?
A null value becomes an empty element, written as <notes/> when Self-close empty elements is ticked and as <notes></notes> when it is not. True and false are written as the text true and false, and numbers are written as they appear.
Are special characters like & and < escaped in the XML?
Yes. Characters that have meaning in XML, such as &, < and >, are written as entities like &amp; and &lt;, and quotes are escaped too, so the output stays well-formed.
Can I remove the XML declaration or change the indentation?
Yes. Untick XML declaration to drop the <?xml version="1.0" encoding="UTF-8"?> line, and use the Indent menu for 2 spaces, 4 spaces, tabs or a minified single line.
Why did my JSON to XML output get wrapped in a root element?
An XML document can have only one root element. When your JSON has several top-level keys or is an array, the converter wraps everything in the element named in the Root element field.

More Developer tools

View all