Format Code Now
/
ConvertJSON to XML Next: CSV to XML ›

JSON to XML Converter

CV-06

Paste JSON and get well-formed XML. Keys become elements, keys that start with @ become attributes, and each array item becomes its own repeated element.

Runs in your browser. Nothing is uploaded.

JSON (optional first line: -- root: name)

XML

How to use it

  1. Paste your JSON into the input box, or open a .json file (up to 5 MB) or drop it on the box.
  2. Leave Root element on Auto, or pick a name to wrap the output in. Choose the indent.
  3. Untick XML declaration if you do not want the <?xml ...?> line at the top.
  4. Copy the XML or download it as an .xml file.

How JSON maps to XML

An object key becomes a child element, a key that starts with @ becomes an attribute, and the key "#text" becomes the text inside the element. An array becomes one element per item, all with the key's name. Numbers and booleans are written as text, and numbers appear exactly as you typed them, so a 20 digit ID is not rounded. The rules match the XML to JSON tool, so converting there and back gives the same XML for ordinary documents.

Choosing the root element

XML needs one root element. With Auto, a top-level object that has a single key (and that key does not hold an array) uses that key as the root. Anything else is wrapped in <root>. Choose another name in the list, or put a first line such as -- root: orders above the JSON, and the output is always wrapped in that name. A top-level array becomes <item> elements inside the root.

Names, nulls and empty arrays

JSON keys can hold characters XML names cannot, such as spaces or a leading digit. Those characters are replaced with an underscore, so "first name" becomes first_name, and a leading underscore is added where needed. null and "" both become an empty element. An empty array produces no element at all. Control characters that XML 1.0 cannot hold are reported as an error instead of being written. Namespaces are not declared for you.

Error messages and what to do

MessageWhat it meansFix
Input is not valid JSON: Unexpected end of input at line 1, column 6The JSON is cut off or has a syntax error, and the position shows where parsing stopped.Fix the JSON at that position, then convert again. The JSON Formatter pinpoints errors too.
The attribute "@x" at a must be a string, number, true, false or nullA key that starts with @ turns into an attribute, and an attribute can only hold a single value, not an object or an array.Remove the @ from the key to make it a child element, or give it a plain value.
Root name "bad name" is not a valid XML name (letters, digits, "_", "-", "." and ":"; it cannot start with a digit)The name on the -- root: line is not allowed as an XML element name.Use letters, digits, underscore, hyphen, dot or colon, and do not start with a digit.
The value at a contains a control character that XML 1.0 cannot holdA string contains a character such as \u0001 that is not allowed anywhere in an XML 1.0 document, even escaped.Remove the character from the JSON value first.

Questions people ask

How do I convert JSON to XML?

Paste the JSON into the input box and the XML appears as you type. Each key becomes an element and each array item becomes a repeated element. Copy the result or download it as an .xml file.

How do I make an XML attribute from JSON?

Start the key with @. In {"book": {"@id": "1", "title": "Dune"}} the id becomes an attribute, so you get <book id="1"> with a <title> child. The attribute value must be a string, number, true, false or null.

How are JSON arrays written in XML?

An array under a key becomes one element per item, all named after the key, so "tag": ["a","b"] gives two <tag> elements. An array inside an array uses <item> for its entries. An empty array produces no element.

What root element does it use?

A top-level object with one key, which is not an array, uses that key as the root. Anything else is wrapped in <root>. You can choose another name from the list or add a -- root: name line at the top to wrap the output in that name.

What happens to JSON keys that are not valid XML names?

Characters XML does not allow in a name, such as spaces or slashes, are replaced with an underscore, and a leading underscore is added if the name starts with a digit. The key "first name" becomes the element first_name.

Does the XML include a declaration?

By default the output starts with <?xml version="1.0" encoding="UTF-8"?>. Untick XML declaration to leave it out, for example when you are pasting the result inside another XML document.

About this tool

The rules mirror XML to JSON. A key that starts with @ becomes an attribute, "#text" becomes the text of the element, every other key becomes a child element, and an array becomes one element per item. With Auto, a top-level object with a single key (that is not an array) provides the root element; anything else is wrapped in <root>, and array items without a name become <item>. Pick a root name from the list, or put a first line such as -- root: orders above the JSON, to always wrap in that name. Changes to know about: keys that are not valid XML names have their invalid characters replaced with _ (and a leading _ added if needed), so "first name" becomes first_name; an empty array produces no element; null and "" both become an empty element; booleans and numbers are written as text, numbers exactly as you typed them. Control characters that XML 1.0 cannot hold are an error. Namespaces are not declared for you.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.