JSON to XML Converter - Convert JSON and XML Online
JSON ↔ XML Converter
Conversion rules
- A key starting with @ becomes an attribute: {"@id": "7"} → <book id="7">.
- "#text" is the text of an element that also has attributes: {"@lang": "en", "#text": "Hi"} → <note lang="en">Hi</note>.
- An array repeats the element under the same name: "tag": ["a", "b"] → <tag>a</tag><tag>b</tag>. An array inside an array is wrapped in <item>. An empty array gives an empty element: "tag": [] → <tag/>.
- Root: if the JSON is an object with a single key, that key becomes the root element; otherwise everything is wrapped in <root>, or in the name you type.
- Names are made valid for XML: invalid characters become _, and a name starting with a digit, dot or hyphen gets a leading _ ("1" → <_1>, "first name" → <first_name>). & < > are escaped; null and "" give an empty element.
- A prefix such as "a:b" is kept only when "@xmlns:a" is declared on that element or an ancestor; otherwise it becomes <a_b>. If two attribute names end up the same after cleaning, the second gets _2: "@a b" and "@a_b" → a_b and a_b_2.
- XML → JSON: repeated sibling elements become an array, an element with only text becomes its value, attributes get @. Values stay strings unless you turn on number and true/false detection.
How JSON maps to XML
JSON and XML both describe trees, but with different building blocks: XML has attributes, mixed text and repeated elements, while JSON has arrays, numbers and booleans. There is no single official mapping between them, so this converter follows one fixed, documented convention. You can predict the output and convert it back.
How to use it
- 1 Paste JSON into the left box, or choose XML → JSON and paste XML. The result updates as you type.
- 2 Pick the indent. For JSON → XML you can also set the root element name and leave out the <?xml?> declaration.
- 3 Copy the result or download it as a .xml or .json file. Clicking the other direction moves the result into the input, so you can check the round trip.
Why a one-item array comes back as a single value
XML has no array type: a list is simply the same element written several times. {"tag": ["a", "b"]} becomes two <tag> elements and returns as an array, but {"tag": ["a"]} becomes one <tag> element, which looks exactly like a plain value and returns as "a". If your code expects an array, wrap single values in one after reading the JSON.
Why numbers come back as strings
In XML everything is text: <year>1813</year> carries no type. By default every value stays a string, because guessing damages real data: a postcode "007" would turn into 7, and an ID like 12345678901234567890 would lose digits. Turn on number and true/false detection only when you know the fields are numeric. Even then only values that print back identically are converted, so "1.50" and "007" stay strings. In the other direction, JSON → XML, numbers are copied exactly as written, so 12345678901234567890 and 1.10 keep every digit; if your browser is too old for that, a warning lists the numbers it changed.
Characters that are not allowed in XML names
A JSON key can be any text; an XML element name cannot. It must not start with a digit, hyphen or dot, and cannot contain spaces, slashes or most punctuation. "2nd floor" becomes <_2nd_floor> and "price/kg" becomes <price_kg>. Two keys that end up with the same name look like a repeated element when converted back, so rename such keys in the source if you need a clean round trip.
Escaping and error messages
The characters & < > in text are written as & < >, and quotes in attributes as ", so the output always parses. If the JSON is broken you see the parser’s own message, such as an unexpected token and its position. For XML the browser’s parser reports the first problem, usually a tag that is not closed, tags closed in the wrong order, or a bare & that should be &.