XML to JSON Converter
About the XML to JSON Converter
The XML to JSON Converter turns XML into JSON and JSON back into XML, in your browser, using the browser's built-in `DOMParser` and a fixed, documented mapping: attributes become prefixed keys, repeated elements become arrays, and text that sits next to attributes or child elements goes under `#text`. Use it to read SOAP responses, RSS and Atom feeds, Maven `pom.xml` files, Android resources, or legacy API payloads in a JSON tool, or to produce XML for a system that still expects it. Conversion runs in your browser and nothing is sent to our servers or analytics. The input is not saved in the page address; it goes into a link only when you click Share, so avoid sharing documents that contain secrets.
Updated: November 13, 2026
Related Tools
How to use the converter
- Paste or drop an `.xml`, `.json`, or `.txt` file into the input area. When the input box was empty, or when you drop a file, the direction is picked from the first non-space character: `<` means XML, `{` or `[` means JSON.
- Use the XML to JSON and JSON to XML buttons to set the direction yourself. The output updates as you type, and Swap moves the output back into the input.
- Indent applies to both outputs: 2 spaces, 4 spaces, or minified. Attr prefix sets the key prefix used for attributes: `@_` (the default), `@`, or `_`.
- XML to JSON options: Always arrays wraps every child element in an array, even when it appears once, so code reading the JSON sees the same shape every time. Infer types turns text such as `42` and `true` into JSON numbers and booleans. Both are off by default.
- JSON to XML options: Root sets the element that wraps the JSON when it does not have exactly one top-level key (default `root`), and XML declaration adds `<?xml version="1.0" encoding="UTF-8"?>` as the first line (on by default).
- Copy the result, or click Save to download it as `converted.json` or `converted.xml`.
The mapping convention
- Root element. The root element's name is the single top-level key: `<note><to>Ana</to></note>` becomes `{"note": {"to": "Ana"}}`.
- Attributes. Each attribute becomes a key made of the prefix and the attribute name: `<price currency="EUR">9.99</price>` becomes `{"price": {"@_currency": "EUR", "#text": "9.99"}}`. Namespace declarations such as `xmlns:dc` are attributes too, so they are kept.
- Text. An element with only text becomes a plain string. An element with no content becomes an empty string. When an element also has attributes or child elements, its text goes under `#text`. Text is trimmed at both ends, and whitespace-only text between elements is dropped.
- Repeated elements. Sibling elements with the same name become an array, in document order: two `<tag>` children become `"tag": ["a", "b"]`. A single child stays a plain value unless Always arrays is on.
- Mixed content. In `<p>Hello <b>world</b> again</p>`, the text pieces are joined with a space under `#text` (`"Hello again"`) and `<b>` becomes a key. Where the text sat relative to the child elements is not kept.
- CDATA, comments, and processing instructions. CDATA sections are kept as plain text and joined with the text around them. Comments, processing instructions, and the XML declaration are dropped.
- Names. Element and attribute names are kept exactly as written, including namespace prefixes such as `dc:title`. Namespace URIs are not resolved.
- Types. Without Infer types, every value is a string. With it, `true` and `false` become booleans, and a number becomes a JSON number only when it converts back to the same text, so `007`, `1.50`, `1e3`, and integers too large to store exactly stay strings.
- JSON to XML is the inverse. Keys starting with the attribute prefix become attributes, `#text` becomes the element's text, arrays repeat the element, `null` and empty strings become empty elements such as `<note/>`, and attributes set to `null` are left out. Arrays nested directly inside arrays are flattened into the same repeated element. A top-level JSON array is wrapped in the root element as repeated `<item>` elements.
Escaping and invalid names
- In text, `&`, `<`, and `>` are written as `&`, `<`, and `>`. In attribute values, `"` and `'` are also escaped (`"`, `'`), and tabs and line breaks are written as character references so they survive a round trip.
- XML names are stricter than JSON keys. A key that starts with a digit, `-`, or `.`, contains a space, or contains characters such as `$` cannot become an element or attribute name. The converter stops with an error that names the key, where it is, and a valid replacement to rename it to, for example `1st` to `_1st` or `first name` to `first_name`. It does not rename keys silently.
- Control characters such as U+0007 cannot appear in XML 1.0 at all, even escaped, so a JSON string containing one is reported as an error.
- A key with a namespace prefix, such as `dc:title`, is written as is, but only when the prefix is declared: add the matching declaration (for example `"@_xmlns:dc": "http://purl.org/dc/elements/1.1/"`) on that element or an ancestor. An undeclared prefix is reported as an error, because XML parsers, including Swap, would reject the output. The `xml:` prefix is always declared.
Privacy and security
XML is parsed with the browser's native `DOMParser`, and JSON with `JSON.parse`, inside your browser tab; no request carries your document, and nothing is sent to our servers or analytics. Browsers do not load external entities or DTDs when parsing this way. As an extra guard against entity-expansion attacks such as the "billion laughs", input with a DOCTYPE internal subset (`<!DOCTYPE name [ ... ]>`, where `<!ENTITY>` definitions live) is rejected with an error; a plain DOCTYPE without brackets is accepted and ignored. Input is limited to 5,000,000 characters, output to 10,000,000 characters, and nesting to 256 levels. Your options are saved in the page address as you change them; the document itself goes into a link only when you click Share. Long input is compressed with lz-string, which is not encryption, so anyone with the link can read it.
Tips and pitfalls
- Shapes change with the data. Without Always arrays, an element that appears once is an object or string and one that appears twice is an array. If code reads the JSON, turn on Always arrays or handle both shapes.
- Round trips. XML to JSON and back gives the same elements, attributes, and text for documents without mixed content, but comments, CDATA markers, the original whitespace, and the order of text between child elements are not restored.
- Choosing a prefix. With the `_` prefix, a JSON key such as `_id` is read as an attribute named `id`. If your data has keys that start with `_`, use `@_` or `@`. In the XML to JSON direction, an attribute and a child element that would map to the same key are reported as an error.
- Parse errors give a position. Invalid XML, such as a missing closing tag or an undeclared namespace prefix, is reported with the line and column the browser gives.
Frequently Asked Questions
- How are XML attributes represented in JSON?
- As keys made of a prefix and the attribute name. The default prefix is `@_`, so `<book id="b1">` becomes `{"book": {"@_id": "b1"}}`. You can switch the prefix to `@` or `_`; the same prefix is used when converting JSON back to XML, so keys that start with it become attributes.
- What happens to repeated elements?
- Sibling elements with the same name become a JSON array in document order. An element that appears once stays a single value unless you turn on Always arrays, which wraps every child element in an array. In the JSON to XML direction, an array repeats the element once per item.
- Are numbers and booleans converted?
- Not by default: XML has no types, so every value is a string. Turn on Infer types to convert `true` and `false` to booleans and numbers to JSON numbers. Only numbers that convert back to the same text are changed, so values like `007` or `1.50` stay strings.
- Are CDATA sections and comments kept?
- CDATA content is kept as ordinary text; the `<![CDATA[ ]]>` markers are not. Comments and processing instructions are dropped. When converting JSON to XML, special characters are escaped instead of being wrapped in CDATA.
- Why is my JSON key rejected when converting to XML?
- XML element and attribute names cannot start with a digit, `-`, or `.`, and cannot contain spaces or most punctuation. The error message names the key, shows where it is, and suggests a valid name such as `_1st` for `1st`. Rename the key in your JSON and the conversion runs.
- Is it safe to paste XML with a DOCTYPE?
- A DOCTYPE without an internal subset is accepted and ignored. A DOCTYPE with an internal subset, the bracketed block where `<!ENTITY>` definitions live, is rejected to block entity-expansion attacks; remove the block and convert again. External entities and DTDs are never fetched.