Skip to content
PithLab

↑↓ to move · Enter to open · Esc to close

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\"]}}"}'

Fields and responseOpenAPI documentGet an API key

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

  1. Paste your JSON, or click Example.
  2. Keys that start with the attribute prefix (default "@_") become attributes. A "#text" key sets the element text.
  3. Set the root element name, used when the JSON does not have a single top-level key.
  4. Choose the indentation and whether to add an XML declaration, then copy or download the XML.

Examples

Object with an attribute

Input

{"book":{"@_id":"42","title":"Dune","author":"Frank Herbert"}}

Output

<book id="42">
  <title>Dune</title>
  <author>Frank Herbert</author>
</book>
Top-level array wrapped in a root element

Input

[{"name":"Ada"},{"name":"Alan"}]

Output

<root>
  <item>
    <name>Ada</name>
  </item>
  <item>
    <name>Alan</name>
  </item>
</root>
Escaping and text with attributes (minified)

Input

{"note":{"@_lang":"en","#text":"Fish & chips < £10"}}

Output

<note lang="en">Fish &amp; chips &lt; £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.