XML to JSON Converter with Attribute Options
Convert XML to JSON with configurable attribute prefix, text key, arrays and namespaces
How the XML to JSON converter maps elements, attributes and text
XML and JSON do not line up one to one — XML has attributes, mixed text and repeated elements, JSON has objects, arrays and plain values — so every XML to JSON converter has to pick conventions. This one uses the widely understood ones and lets you change them:
Each element becomes a key named after the tag. An element that contains only text becomes a plain string, so <title>Dune</title> turns into "title": "Dune". An empty element becomes an empty string. Attributes become keys with a prefix, "@" by default, so <book id="7"> produces "@id": "7". When an element has attributes or child elements and also text, the text goes under a text key, "#text" by default. Sibling elements with the same name are collected into an array, so two <book> elements become "book": [ {...}, {...} ]. Comments and processing instructions are dropped, and CDATA sections are treated as text.
All values come out as strings. XML has no types without a schema, so 007 stays "007" rather than being guessed into the number 7; cast the fields you know are numeric in your own code. Note too that mixed content loses its ordering: in <p>Hello <b>world</b> again</p> the text pieces are joined under #text and the <b> element becomes a separate key, so this converter suits data-style XML better than document-style markup.
Options: attribute prefix, text key, arrays, whitespace and namespaces
Attribute prefix and text key can be set to whatever your code expects — "_" and "__text", "$" and "_", or an empty attribute prefix if you prefer attributes to look like ordinary keys (watch for clashes with child elements of the same name).
The always-array toggle solves the classic XML to JSON headache: an element that appears once becomes an object, but the same element appearing twice becomes an array, so code that loops over it breaks on the single-item case. With always-array on, every child element is wrapped in an array, which makes the shape predictable at the cost of extra [0] lookups.
Ignore whitespace (on by default) drops the indentation between elements and trims text; switch it off when leading or trailing spaces inside text are meaningful. Keep namespaces leaves prefixed names such as soap:Envelope and the xmlns declarations in place; switch it off to strip prefixes and xmlns attributes for cleaner keys, as long as no two namespaces use the same local name in one element.
Convert XML to JSON online, privately
The XML is parsed with your browser's built-in DOMParser, the same strict XML parser the browser uses for SVG and RSS. Nothing is uploaded, so it is safe for API responses, config files and exports containing customer data. Because the parser is strict, malformed input fails with the line and column of the problem — an unclosed tag, a stray ampersand that should be &, or two root elements — rather than producing half-converted output. HTML is not XML: unclosed <br> or <img> tags will fail, so use an HTML-specific tool for web pages.
The result is pretty-printed with two-space indentation. Copy it or download it as a .json file. To go the other way, the JSON to XML converter uses the same @ and #text conventions, so data converted here can be turned back into equivalent XML.
How to use the XML to JSON Converter with Attribute Options tool
- 1
Paste your XML
Paste a document or fragment with a single root element.
- 2
Adjust the options
Set the attribute prefix and text key, and toggle always-array, whitespace and namespace handling.
- 3
Read the JSON or the error
The JSON updates as you type; malformed XML shows the line and column of the problem.
- 4
Copy or download
Copy the JSON or download it as a .json file.
Frequently asked questions
How are XML attributes represented in JSON?
As keys with a prefix, "@" by default: <item id="3"> becomes {"item": {"@id": "3"}}. You can change or remove the prefix in the options.
Why is a single element an object but two are an array?
Repeated siblings are grouped into an array, and a lone element has nothing to group with. Turn on always-array to wrap every child element in an array so the shape never changes.
Why are numbers and booleans strings?
XML carries no type information without a schema, so values are kept as written. That preserves leading zeros and long IDs; convert specific fields to numbers in your code.
Is my XML uploaded?
No. Parsing uses the browser's DOMParser and the conversion runs locally, so your data never leaves your device.
Can I convert the JSON back to XML?
Yes. The JSON to XML tool uses the same attribute prefix and text key conventions, so the output can be converted back into equivalent XML.
Related on TechLogHub
Related Converter Tools
Unix Timestamp Converter with Milliseconds
Convert Unix timestamps to human-readable dates and vice versa
Cron Expression Generator with Next Run Times
Build, explain and test cron expressions, with the next run times in UTC and your timezone
ENV to JSON
Convert .env files to JSON format
ENV to Docker Compose
Convert .env files to docker-compose environment snippet
Query String to JSON
Parse URL query strings into JSON objects
JSON to Query String
Convert JSON objects to URL query strings
Get the next tool before you need it
One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.

