JSON to XML Converter
Turn JSON objects into indented, valid XML.
Use via API
Convert JSON to XML. Keys with the attribute prefix (default @_) become attributes; arrays become repeated elements. Send JSON, get JSON back.
POST https://pithlab.com/api/v1/tools/json-to-xml
curl https://pithlab.com/api/v1/tools/json-to-xml \
-H "Authorization: Bearer $PITHLAB_KEY" \
-H "Content-Type: application/json" \
-d '{"input":"{\"book\":{\"@_id\":\"1\",\"title\":\"Dune\",\"authors\":[\"Frank Herbert\"]}}"}'About the JSON to XML
Convert JSON to well-formed XML in your browser. Prefixed keys become attributes, arrays become repeated elements and special characters are escaped for you.
How to use the JSON to XML
- Paste your JSON, or click Example.
- Keys that start with the attribute prefix (default "@_") become attributes. A "#text" key sets the element text.
- Set the root element name, used when the JSON does not have a single top-level key.
- Choose the indentation and whether to add an XML declaration, then copy or download the XML.
Examples
Input
{"book":{"@_id":"42","title":"Dune","author":"Frank Herbert"}}Output
<book id="42"> <title>Dune</title> <author>Frank Herbert</author> </book>
Input
[{"name":"Ada"},{"name":"Alan"}]Output
<root>
<item>
<name>Ada</name>
</item>
<item>
<name>Alan</name>
</item>
</root>Input
{"note":{"@_lang":"en","#text":"Fish & chips < £10"}}Output
<note lang="en">Fish & chips < £10</note>
Frequently asked questions
How do I create attributes?
Start the key with the attribute prefix, "@_" by default. {"book":{"@_id":"42"}} becomes <book id="42"></book>. Use a "#text" key for the element text alongside attributes.
How are arrays converted?
An array becomes one element per item, all with the key's name, so {"tag":["a","b"]} gives <tag>a</tag><tag>b</tag>. An array at the top level, or nested directly in another array, uses <item> elements.
When is the root element added?
XML needs exactly one root element. If your JSON is an object with a single key holding an object or value, that key is the root. Otherwise everything is wrapped in the root name you choose.
Why do I get a not a valid XML element name error?
XML names cannot contain spaces or start with a digit or most punctuation, so a key like "first name" or "2024" cannot become an element. Rename the key in your JSON and try again.
How are null and empty values written?
null becomes a self-closing element such as <middleName/>, and an empty string or empty object becomes an empty element.