Free tool
XML to JSON Converter
Paste XML and get JSON as you type. Attributes become prefixed keys, repeated elements become arrays, and you choose the text key, the prefix and whether numbers stay strings. It converts JSON back to XML too.
Free · no sign-up · runs in your browser, nothing is uploaded
Attributes become keys starting with "@". Repeated elements become arrays; single ones stay objects. Values become numbers or booleans only when the JSON prints back the same text, so 02108 and 1.10 stay strings.
JSON appears here as you type.
How XML maps to JSON
XML has elements, attributes, text and order; JSON has objects, arrays and values. There is no single official mapping, so this is what the converter does with the default options:
| XML | JSON | Note |
|---|---|---|
| <name>Ada</name> | "name": "Ada" | An element with only text becomes a value. |
| <user id="7"/> | "user": { "@id": 7 } | Attributes become keys with the prefix. |
| <price cur="USD">9.5</price> | "price": { "@cur": "USD", "#text": 9.5 } | Text next to attributes goes under the text key. |
| <b>1</b><b>2</b> | "b": [1, 2] | Repeated siblings become an array. |
| <p>Hi <b>x</b> there</p> | "p": { "b": "x", "#text": "Hi there" } | Text around child tags is joined; where it sat is lost. |
| <note/> | "note": "" | An empty element becomes an empty string. |
| <![CDATA[<x>]]> | "<x>" | CDATA becomes plain text. |
| &lt; | "<" | Entities are decoded. |
| <!-- c --> | (dropped) | Comments and processing instructions are not kept. |
Other tools follow similar rules with different names. Python's xmltodict and Json.NET both use @ for attributes and #text for text, so the defaults here produce the same shape. yq uses its own attribute prefix and puts text under +content; both can be changed with flags.
Pick options for the JSON you need
- Feeding a typed client or a schema? Turn on Always use arrays. Then
order.items.itemis always an array, even when an order has one item, and your code needs no special case. - Matching Python or .NET output? Keep the
@prefix and#text. That is the xmltodict and Json.NET convention. - PHP-style output? Choose Grouped. Each element gets an
@attributesobject, like PHP'sjson_encode(simplexml_load_string($xml))produces. - Only want the data? Choose Ignore for attributes and turn on Strip namespaces. SOAP and RSS responses get much shorter.
- Need exact text? Turn off Numbers and booleans so every value stays a string, as it was in the XML.
Convert XML to JSON in code
JavaScript and Node.js with fast-xml-parser (the library this page uses):
import { XMLParser } from "fast-xml-parser";
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@",
isArray: (name) => name === "item", // force arrays for chosen elements
});
const json = JSON.stringify(parser.parse(xml), null, 2);Python with xmltodict, and back again with unparse():
import json, xmltodict
data = xmltodict.parse(xml, force_list=("item",)) # attributes as @key, text as #text
print(json.dumps(data, indent=2))
xml_again = xmltodict.unparse(data, pretty=True)Command line with yq:
yq -p xml -o json input.xml > output.json yq -p json -o xml input.json > output.xml
C# with Json.NET:
var doc = new XmlDocument(); doc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented); XmlDocument back = JsonConvert.DeserializeXmlNode(json);
JSON to XML: what to watch for
Going the other way, some JSON has no direct XML form. The converter handles these cases and tells you what it changed:
- Several top-level keys, or a top-level array. XML needs one root element, so the JSON is wrapped in
<root>(rename it in the options). Array entries become<item>elements. - Keys that are not valid XML names. Names cannot contain spaces or start with a digit, and names starting with "xml" are reserved.
first namebecomesfirst_nameand1stbecomes_1st. - Special characters.
&,<and>in values are escaped, so the output is always well-formed. - null. Becomes an empty element,
<key/>.
To check or pretty-print the result, open it in the XML formatter and validator. For other formats, try YAML to JSON or JSON to CSV, and browse API tools on DevHunt.
More free developer tools
FAQ
How do I convert XML to JSON?+
Paste the XML into the box above and the JSON appears as you type. Choose how attributes are written (prefixed keys like @id, grouped in an @attributes object, or left out), the key for element text, and whether every child should be an array. Then copy or download the JSON.
How are XML attributes represented in JSON?+
JSON has no attributes, so converters turn them into keys. The common convention, used by xmltodict and Json.NET, is the attribute name with an @ prefix: <user id="7"> becomes {"user": {"@id": 7}}. You can also pick _, -, $ or no prefix, or group all attributes under one "@attributes" key.
Why is one element an object but two of the same element an array?+
A converter only sees the XML in front of it. One <item> becomes an object; two <item> siblings become an array. If your code expects an array either way, turn on Always use arrays, so every child element is wrapped in an array no matter how many there are.
Why are some numbers kept as strings?+
XML has no types; everything is text. With Numbers and booleans on, a value becomes a JSON number or boolean only when it would print back as exactly the same text. So 42 and true convert, but 02108 (a ZIP code), 1.10 (a price) and IDs too large for a JavaScript number stay strings, because converting them would change them.
Is converting XML to JSON lossless?+
Not fully. Comments and processing instructions are dropped, the order of text mixed with tags is lost, and without a prefix you cannot tell an attribute from a child element. For data such as API responses and config files the result is usually complete. To keep everything, store the original XML.
Can I convert JSON back to XML?+
Yes. Switch the direction to JSON → XML, or click Swap to send the current output back. Keys with your attribute prefix become attributes, the text key becomes element text, and arrays become repeated elements. If the JSON has several top-level keys or is an array, it is wrapped in a root element you can name.
What happens to namespaces?+
By default prefixes stay in the key names (soap:Envelope) and xmlns declarations become attributes. Turn on Strip namespaces to drop the prefixes and the xmlns attributes, which gives shorter keys. Two elements that differ only by namespace then share a name.
Sources: xmltodict · Json.NET: Converting between JSON and XML · yq: Working with XML · fast-xml-parser · W3C XML 1.0. Checked 2026-10-29.