Skip to content
MoneyDeck

JSON to XML Converter

Convert JSON to well-formed XML and XML back to JSON, with attributes

Updated · Free, no signup

{"order": {...}} becomes <order>…</order> instead of <root><order>…</order></root>.

Output

<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001">
  <customer>Ada Lovelace</customer>
  <paid>true</paid>
  <items>
    <sku>BK-1</sku>
    <qty>2</qty>
  </items>
  <items>
    <sku>PN-7</sku>
    <qty>1</qty>
  </items>
  <note/>
</order>

Validation

Valid JSON

XML elements

10

XML attributes

1

About the JSON to XML Converter

This JSON to XML converter turns a JSON document into well-formed, indented XML, and converts XML back into JSON. It is useful when a REST API speaks JSON but a legacy system, SOAP service, RSS feed, sitemap, Android resource or enterprise integration expects XML — or the other way round, when you need to read an XML response in JavaScript.

It follows the widely used mapping conventions: each JSON key becomes an element, arrays become repeated sibling elements with the same name, keys starting with @ become XML attributes, and a #text key holds an element’s text when it also has attributes. Converting XML to JSON reverses this, so attributes appear as "@name" and repeated elements are collected into arrays. Characters such as <, > and & are escaped, and keys that are not legal XML names (spaces, leading digits) are fixed with underscores.

XML has a single root element, so you can name the wrapper (default "root"), or let a JSON object with exactly one top-level key supply the root automatically. XML has no data types, so values come back as strings unless you turn on number and boolean detection. Everything runs locally in your browser.

How to use the json to xml converter

  1. 1Choose JSON → XML or XML → JSON.
  2. 2Paste your document into the input.
  3. 3For JSON → XML, set the root element name and declaration options.
  4. 4For XML → JSON, decide whether to detect numbers and booleans.
  5. 5Copy the converted output.

Formula and method

JSON → XML: the document is walked recursively. An object becomes an element whose child elements are its keys; an array repeats its parent’s element once per item (nested arrays use <item>); strings, numbers and booleans become escaped text; null becomes an empty element such as <note/>. Keys beginning with @ become attributes and #text becomes the element’s own text. Names are made XML-safe: characters outside letters, digits, _, - and . become _, and names starting with a digit or "xml" get a leading _.

XML → JSON: the XML is checked for well-formedness (matching tags, a single root, quoted attributes), comments, processing instructions and DOCTYPE are skipped, CDATA and entities are decoded, then each element becomes a value: plain text for a simple element, null for an empty one, or an object with "@attribute" keys, child keys (repeated children become arrays) and "#text" for mixed text.

Worked examples

Order JSON with an attribute and a repeated array

The single top-level key "order" becomes the root. "@id" becomes the id attribute, the two-item array produces two sibling <items> elements, and null becomes the empty element <note/>. That is 10 elements and 1 attribute.

XML book record to JSON

Attributes become "@id" and "@lang", the two <tag> elements are grouped into an array, and <price> has both an attribute and text, so its text is stored under "#text". Values stay strings because number detection is off.

Escaping and invalid key names

With two top-level keys, the custom root <data> wraps everything. The space in "a b" becomes an underscore, "1st" gets a leading underscore because XML names cannot start with a digit, and < and & are escaped as &lt; and &amp;.

Frequently asked questions

How do I convert JSON to XML?+

Paste the JSON with the direction set to JSON → XML. Keys become elements, arrays become repeated elements, and the result is wrapped in a single root element as XML requires. Copy the indented XML from the output.

How are JSON arrays represented in XML?+

XML has no array type, so each item is written as a sibling element with the same name: "tag": ["a", "b"] becomes <tag>a</tag><tag>b</tag>. Converting back, repeated sibling elements are collected into a JSON array again.

How do I create XML attributes from JSON?+

Prefix the key with @, as in {"price": {"@currency": "USD", "#text": "9.99"}}, which produces <price currency="USD">9.99</price>. This @ / #text convention is shared by many converters and libraries.

Why are all values strings after converting XML to JSON?+

XML stores everything as text, so the converter cannot know whether "007" is a number or an ID. Turn on "Detect numbers and booleans" to convert plain numbers and true/false; numbers with leading zeros are kept as strings.

Why does XML need a root element?+

A well-formed XML document must have exactly one top-level element containing everything else (XML 1.0, section 2.1). JSON can have several top-level keys or be an array, so the converter wraps such data in a root element you can name.

Related tools