JSON to XML Converter

Convert JSON to XML online. Paste JSON, even with comments or trailing commas, and well-formed XML appears as you type. Keys starting with @ become attributes, arrays become repeated elements, and keys that are not valid XML names are fixed for you. Numbers are copied exactly, and nothing is uploaded.

XML output

        

How to convert JSON to XML

  1. Paste or open the JSON. You can drop a file onto the input box. If the JSON has comments, trailing commas, single quotes, unquoted keys or Python-style True/None, it is repaired first and the fixes are listed under the buttons.
  2. Pick the shape of the XML. Choose the root element name, how arrays are written, and whether keys starting with @ become attributes. The defaults follow the same conventions as xmltodict, Json.NET and the XML to JSON converter, so a round trip gives you the XML you started with.
  3. Copy or download the XML. The output is always well-formed: names are valid, special characters are escaped, and there is exactly one root element. Press Swap to check it by converting it back to JSON.

Worked example: JSON to XML and back

{
  "order": {
    "@id": "A-1001",
    "ref": 1453729827150749696,
    "total": { "@currency": "EUR", "#text": 42.50 },
    "line": [
      { "@sku": "PEN", "qty": 2 },
      { "@sku": "INK", "qty": 1 }
    ],
    "note": "Leave at door & ring <twice>",
    "2nd address": null
  }
}

With the default settings this becomes:

<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001">
  <ref>1453729827150749696</ref>
  <total currency="EUR">42.50</total>
  <line sku="PEN">
    <qty>2</qty>
  </line>
  <line sku="INK">
    <qty>1</qty>
  </line>
  <note>Leave at door &amp; ring &lt;twice&gt;</note>
  <_2nd_address/>
</order>

The single top-level key order became the root, so no extra <root> wrapper was needed. @id and @currency became attributes, and #text became the text of <total>. The line array became two sibling <line> elements. ref keeps all 19 digits and 42.50 keeps its trailing zero. Converters that go through JSON.parse print 1453729827150749700 and 42.5. "2nd address" is not a legal XML name, so it was renamed to _2nd_address. Convert the result back with the XML to JSON converter and you get the original JSON again, apart from the renamed key and null.

JSON to XML mapping conventions

JSONXMLNotes
"a": "x"<a>x</a>Numbers and booleans are written as text exactly as in the JSON
"a": {"@id": 7}<a id="7"/>Only scalar values can be attributes. An @key holding an object stays an element
"a": {"@id": 7, "#text": "x"}<a id="7">x</a>Text key is configurable
"b": [1, 2]<b>1</b><b>2</b>Or <b><item>1</item><item>2</item></b> with wrap in item elements
"b": []nothing, or <b/>Lossy. An empty repeated list has no elements to write
[[1, 2], [3]]<item><item>1</item>...</item>Nested arrays are always wrapped so the grouping survives
null, "", {}<a/>Lossy. XML cannot tell these apart without a schema or xsi:nil
"dc:title", "@xmlns:dc"<dc:title>, xmlns:dc="..."Prefixes are kept. You get a warning if a prefix is never declared
"2024 sales"<_2024_sales> or <entry key="2024 sales">Names must start with a letter or _, and spaces are not allowed
"#cdata": "<b>"<![CDATA[<b>]]>]]> inside the text is split safely
"#comment": "x"<!--x-->Matches the XML to JSON converter's comment option
true, 42true, 42Lossy. Types become text. Use XML to JSON with type inference to get them back

Escaping and CDATA

In element text, &, < and > are written as &amp;, &lt; and &gt;. Attribute values also escape " as &quot; and ' as &apos;. Tabs and newlines in attributes are written as character references, so they are not normalised away. XML 1.0 does not allow most control characters (U+0000 to U+001F, except tab, newline and carriage return), even when escaped, so they are removed. If a string holds HTML or another XML fragment, set CDATA to strings with < or &. The fragment is then kept readable inside <![CDATA[ ... ]]> and is not escaped. To a parser, both forms are the same text.

Mixed content is not round-trippable

XML such as <p>Hello <b>world</b>!</p> mixes text and elements in a particular order. JSON objects are unordered key/value maps, so {"b": "world", "#text": "Hello !"} cannot say where the text goes. The converter writes the text first, followed by the child elements. If you need document-style XML such as XHTML, DocBook or Word XML, generate it with a template or XSLT instead.

Convert JSON to XML in code

Python (xmltodict)

import json, xmltodict          # pip install xmltodict

data = json.load(open("order.json"))
xml = xmltodict.unparse(data, pretty=True)          # needs exactly one top-level key
# no single root?  xmltodict.unparse({"root": data}, pretty=True)

Note that json.load keeps big integers exact in Python, but xmltodict expects lists to sit directly under a key. A top-level list must be wrapped first.

JavaScript / Node.js (fast-xml-parser)

import { XMLBuilder } from "fast-xml-parser";

const builder = new XMLBuilder({
  ignoreAttributes: false,
  attributeNamePrefix: "@",
  textNodeName: "#text",
  format: true,
});
const xml = builder.build(JSON.parse(json));   // JSON.parse rounds integers above 2^53

Java (Jackson XmlMapper or org.json)

// Jackson (jackson-dataformat-xml)
JsonNode tree = new ObjectMapper().readTree(json);
String xml = new XmlMapper().writer().withRootName("order").writeValueAsString(tree);

// org.json: no attributes, arrays become repeated elements
String xml2 = XML.toString(new JSONObject(json), "order");

C# (Json.NET)

// "@" keys become attributes, "#text" text, "#cdata-section" CDATA
XmlDocument doc = JsonConvert.DeserializeXmlNode(json, "root");
string xml = doc.OuterXml;

The second argument adds a root element when the JSON has several top-level keys. Without it, Json.NET throws an exception for that kind of JSON.

Command line (yq, xq)

# mikefarah/yq v4 (attribute keys use the "+@" prefix by default)
yq -p json -o xml --xml-attribute-prefix @ --xml-content-name '#text' order.json

# xq from the Python "yq" package: edit with jq syntax, write XML with -x
xq -x '.order' order.xml

n8n

The n8n XML node has a JSON to XML mode based on xml2js. It expects attributes under a $ key and text under _, and its Root Name option plays the same role as Root here. If your data uses @ keys, set Attribute Key to @ or rename the keys in a Set or Code node first.

Frequently asked questions

How do I convert JSON to XML?

Paste your JSON or open a .json file above and the XML appears as you type. Each key becomes an element, arrays become repeated elements, keys starting with @ become attributes and #text becomes the element's text. Copy the XML or download it as an .xml file. The conversion runs in your browser.

What is the root element of the XML?

An XML document needs exactly one root element. If your JSON is an object with a single key, such as {"catalog": {...}}, that key becomes the root. Otherwise the whole value is wrapped in the root name you choose, which is root by default.

How are JSON arrays converted to XML?

With Repeat parent key, {"book": [a, b]} becomes two <book> elements next to each other. With Wrap in item elements, it becomes one <book> element containing <item> children. Top-level arrays and arrays nested directly inside arrays always use item elements, because they have no key to repeat.

What happens to keys that are not valid XML names?

XML names cannot start with a digit, hyphen or dot, and cannot contain spaces or most punctuation. By default invalid characters are replaced with _ and a leading _ is added when needed, so "2024 sales" becomes <_2024_sales>. Choose the key attribute option to write <entry key="2024 sales"> instead and keep the original name.

Are special characters escaped?

Yes. In text, & < and > become &amp; &lt; and &gt;. In attribute values, " and ' are also escaped as &quot; and &apos;. Characters that XML 1.0 forbids, such as most control characters, are removed. You can also write strings that contain markup as CDATA sections.

Does it handle invalid or messy JSON?

Yes. Comments, trailing commas, single quotes, unquoted keys, Python True/False/None and similar problems are repaired before conversion, and the page lists what it fixed. Numbers are copied as written, so an ID like 1453729827150749696 is not rounded.

Is my JSON uploaded to a server?

No. Parsing and XML generation both happen in JavaScript on this page. Your data never leaves your device.