How to use the xml to json converter
- Paste or type your XML markup into the input area.
- Adjust the attribute prefix if your codebase uses a convention other than
@. - Change the text content key if you prefer something other than
#textfor mixed content. - Toggle Pretty print off when you need compact JSON for APIs or storage.
- Copy the resulting JSON from the output area.
How the XML to JSON mapping works
The converter follows a predictable set of rules so you always know what shape the output will take. Each XML element becomes a JSON object key. If an element contains only text, its value is a plain string rather than a nested object.
Attributes are stored as sibling keys using the configured prefix. For example, <b x="2">t</b> with the default prefix @ produces {"@x": "2", "#text": "t"}. The #text key appears when an element has character data plus attributes or child elements (mixed content, such as <a>hello <b>x</b></a>); an element with only text stays a plain string. Text pieces around child elements are trimmed and joined with a single space.
When multiple sibling elements share the same tag name, they are collected into a JSON array automatically. A single child stays as an object or string, but two or more children named <item> become [...]. This mirrors how most REST APIs expect repeated fields.
Supported XML features
The parser handles the constructs found in typical configuration files, API responses and data exports:
- Elements and nesting to any depth.
- Attributes with single or double quotes.
- Self-closing tags like
<br/>, which map to empty strings when they have no attributes. - CDATA sections whose content is inserted verbatim without entity decoding.
- Comments and the
<?xml ... ?>declaration, both silently ignored. - Entity references:
<,>,&,",', decimalAand hexadecimalA.
Input that is not well-formed gets a clear error instead of silently wrong JSON: mismatched or unclosed tags (with line, column and position), more than one root element, text outside the root, duplicate attributes, invalid numeric character references such as �, and <!DOCTYPE> declarations, which are not supported (remove the line first) because they can define entities this tool does not expand.
Common conversion examples
| XML input | JSON output (compact) |
|---|---|
<r/> | {"r":""} |
<r>text</r> | {"r":"text"} |
<r a="1">t</r> | {"r":{"@a":"1","#text":"t"}} |
<r><i>1</i><i>2</i></r> | {"r":{"i":["1","2"]}} |
<r><a><b/></a></r> | {"r":{"a":{"b":""}}} |
These patterns cover the vast majority of real-world documents you will convert xml to json.
Choosing the right prefix and text key
Different frameworks expect different conventions. The default @ prefix matches libraries like xml2js, while some internal tools prefer an underscore _ or dollar sign $. Changing the prefix here avoids a search-and-replace step after conversion.
The text key matters only for mixed content where an element has character data together with attributes or child elements. If your downstream code expects $t or _text instead of #text, update the field before converting. Elements that contain only text never use this key; their value is simply the string itself.
Integrating with other format tools
Once you have JSON, you may need to transform it further. Use the json to yaml converter when your target system reads YAML configuration files. If you are building typed clients, the json to typescript tool generates interfaces directly from the JSON structure. For inspecting deeply nested results, the JSON viewer provides a collapsible tree. You can also tidy up source markup first with the XML formatter before running the conversion.
Worked examples
| Example | Input | Result |
|---|---|---|
| Attributes and text | XML input: <root><a>1</a><b x="2">t</b></root>; Attribute prefix: @; Text content key: #text; Pretty print: no | {"root":{"a":"1","b":{"@x":"2","#text":"t"}}} |
| Repeated elements become arrays | XML input: <r><i>1</i><i>2</i></r>; Attribute prefix: @; Text content key: #text; Pretty print: no | {"r":{"i":["1","2"]}} |
| Self-closing tag | XML input: <r/>; Attribute prefix: @; Text content key: #text; Pretty print: no | {"r":""} |
| CDATA and entities | XML input: <msg><![CDATA[Hello <world>]]></msg>; Attribute prefix: @; Text content key: #text; Pretty print: yes | { / "msg": "Hello <world>" / } |
FAQ
Does this xml to json converter upload my data?
No. The parsing and conversion happen entirely inside your browser. Nothing is sent to any server, making it safe for sensitive configuration files or API payloads.
Why do repeated elements become arrays?
JSON objects cannot have duplicate keys. When the parser encounters two or more sibling elements with the same tag name, it collects them into an array so no data is lost.
What happens to XML comments and the declaration?
Comments and the processing instruction are stripped during tokenization. They carry no data value in JSON, so they are safely ignored. A declaration is different: it is rejected with an error asking you to remove it.
Can I change how attributes appear in the JSON?
Yes. The attribute prefix field lets you choose any string. Set it to an empty value if you want attribute names to appear without a prefix, though that risks collisions with child element names.
How are CDATA sections handled?
The content inside is treated as raw text. Entities within CDATA are not decoded, matching the XML specification.
What causes a mismatched tag error?
If a closing tag does not match the most recently opened tag, the parser stops and reports the line, column and position of the closing tag and where the open tag started. A document that ends with unclosed elements reports where the unclosed tag was opened.
Is there a size limit for the XML input?
There is no hard limit enforced by the tool. Very large documents may slow down your browser tab, but typical API responses and config files process instantly.