JSON to XML
Convert JSON to XML
About JSON to XML
Converts JSON into XML following the compact convention of the xml-js library: an _attributes object becomes element attributes, _text becomes the text content, and arrays become repeated elements. It is the inverse of the XML to JSON tool.
When you would use it
- Building an XML request body for a SOAP or legacy API from data held as JSON.
- Converting JSON back to XML after editing data that was converted the other way.
- Generating XML configuration or sitemap snippets from a script's JSON output.
How to use it
- Paste JSON (JSON5 syntax is accepted) into the left field.
- Read the XML on the right.
- Copy the result.
Things worth knowing
- Plain keys become elements and plain values their text: {"a":"hi"} gives <a>hi</a>. Use {"a":{"_attributes":{"id":"1"},"_text":"hi"}} to get <a id="1">hi</a>.
- XML needs a single root element, so the JSON object should have exactly one top-level key. Several keys produce several root elements, which is not a well-formed document.
- Keys must be valid XML names: they cannot start with a digit or contain spaces. Keys such as "1st" or "first name" produce invalid XML.
- Numbers, booleans and null are written as text; null and empty objects give empty elements. The output is on one line, so run it through the XML formatter to indent it.
Frequently asked questions
- Why does the output have no XML declaration?
- One is only written when the JSON has a _declaration key, for example {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"root":{...}}.
- How do I set attributes on an element?
- Give the element an object value with an _attributes key holding the attributes and, if needed, a _text key for its text content.