XML to JSON Converter
Convert XML to JSON in the browser: validated first with a line number on errors, attributes under an @_ prefix, repeated elements as arrays.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the XML to JSON Converter?
Paste XML on the left and read the equivalent JSON on the right. The document is validated before anything else happens, so a missing quote or an unclosed tag produces an error message with the line number instead of half-converted output you cannot trust.
Attributes are kept under an @_ prefix, so <order id="A-17"> becomes "@_id": "A-17" inside the order object. A toggle drops attributes entirely when you only care about the element text. Elements that repeat under the same parent come out as a JSON array; a lone element comes out as a plain value.
Parsing uses fast-xml-parser and runs entirely in your browser. The XML you paste, whether it is an API response, a config file or an invoice, never leaves the page, and the Copy JSON button appears as soon as the input is valid.
- Validation before parsing, with the line number in every error
- Attributes under an @_ prefix, with a toggle to drop them
- Repeated elements become JSON arrays automatically
- Numeric element text parsed into JSON numbers
- Everything runs in the page; the XML is never uploaded
Updated
How to use the XML to JSON Converter
- Paste your XML into the left box. The sample order shows the expected shape.
- Fix anything the validator reports; the message names the problem and the line it starts on.
- Decide whether to keep attributes. The @_ toggle switches them on and off and the JSON updates immediately.
- Read the JSON on the right and check how repeated elements came out.
- Click Copy JSON to put the result on your clipboard.
Worked examples
Attributes and numeric text
Input
<book id="42"><title>Dune</title><year>1965</year></book>
Output
{"book": {"title": "Dune", "year": 1965, "@_id": "42"}}The id attribute moves under @_id and stays a string, while the element text 1965 looks numeric and is parsed into a JSON number. Turn the attribute toggle off and @_id disappears.
One element versus two
Input
<list><li>alpha</li><li>beta</li></list>
Output
{"list": {"li": ["alpha", "beta"]}}Two li elements become an array. Delete one and li collapses to the plain string "alpha", which is the classic XML-to-JSON trap: consuming code should accept both shapes.
Invalid input stops early
Input
<order><item>Widget</order>
Output
An error naming the unclosed item tag and its line, no JSON
The validator runs before the parser, so broken markup is reported instead of being guessed at. Fix the tag and the JSON appears.
Background and insight
XML and JSON describe data with different vocabularies, and the gap is wider than it looks. XML, a W3C recommendation since 1998, grew out of SGML and was built for documents: an element can carry attributes, child elements and loose text all at once, and the order of children is part of the meaning. JSON, standardized in RFC 8259, has exactly six shapes: objects, arrays, strings, numbers, booleans and null. Nothing in JSON corresponds to an attribute, and object keys carry no order.
Because of that mismatch there is no single correct mapping, only conventions. The @_ prefix this converter uses is a widely adopted one: attributes become keys that cannot collide with child element names. The awkward case is repetition. A JSON consumer wants a list to always be an array, but an XML parser cannot know that <item> is a list until it sees a second one, which is why a one-item order here produces an object where a two-item order produces an array. Code that reads converted feeds should handle both.
Mixed content is the other casualty. In <p>Hello <b>world</b></p> the text and the bold element interleave, and JSON has no way to say that. Converted documents of that kind put loose text under a #text key and give up the interleaving, which is fine for data-shaped XML such as RSS, sitemaps and SOAP payloads, and wrong for prose.
Common use cases
Reading API responses
Turn a SOAP envelope or a legacy XML endpoint reply into JSON you can paste into tests and fixtures.
Feeds and sitemaps
Convert an RSS or Atom feed to JSON to inspect entries or feed them to code that only speaks JSON.
Config archaeology
See a pom.xml, web.config or Android manifest as a nested object to understand its structure at a glance.
Debugging exports
Check which values an XML export really contains, with attributes separated clearly from element text.
Tips and limitations
- Element text that looks numeric becomes a JSON number, so a part code like 0042 loses its leading zeros. Attribute values are not converted and stay strings.
- An element that carries both attributes and text puts the text under a #text key; look there before assuming the value vanished.
- Empty elements such as <note/> convert to an empty string, not null.
- Namespace prefixes are kept as part of the key, so <soap:Body> becomes "soap:Body". Quote the whole key when you access it in code.
- This page goes one way. To go from JSON toward a tree you can read, format the JSON side with the JSON tools and the XML side with the XML Beautifier.
Sources and further reading
- Extensible Markup Language (XML) 1.0 (Fifth Edition), W3C. Elements, attributes and mixed content, the parts JSON cannot express
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format, IETF
- fast-xml-parser, GitHub. The parser this page runs, including the @_ attribute convention
Frequently asked questions
How are XML attributes represented in the JSON output?
Each attribute becomes a key with an @_ prefix inside its element's object, so <item sku="X1"> produces "@_sku": "X1". The prefix keeps attributes from colliding with child elements that share a name. If you only need element text, switch the attribute toggle off and they are omitted.
Why does the same element sometimes convert to an array and sometimes to an object?
JSON needs to know whether something is a list, and XML never says so. When a tag appears twice under one parent the converter emits an array; when it appears once it emits a single value. Code that consumes converted XML should normalize with something like Array.isArray(x) ? x : [x].
What happens when the XML has a syntax error?
The document is checked by a validator before parsing. An unclosed tag, a mismatched closing tag or a stray angle bracket produces a message that names the problem and the line it starts on, and no JSON is produced until the input is fixed.
Does converting XML to JSON preserve the order of elements?
Sibling elements with the same name keep their order inside their array. Order between differently named siblings is not guaranteed, because JSON object keys are unordered by definition. Documents where interleaving matters, such as prose with inline markup, do not round-trip through JSON.
Are numbers in the XML converted to JSON numbers?
Element text that parses as a number is emitted as a JSON number, so <year>1965</year> becomes 1965. Attribute values are left as strings. If a value like a zip code or an ID must stay a string, treat it as one in your own code after converting.
Can I convert JSON back to XML on this page?
No, this tool goes from XML to JSON only. The reverse direction needs decisions the JSON cannot answer, such as which keys should become attributes and what the root element is called, so a one-click tool would guess. For restructuring JSON itself, the JSON formatter with JSONPath queries is the better fit.
Is there an in-depth guide to the XML to JSON Converter?
Yes. “Unit Conversion Factors: Exact Definitions, Gallons and Temperature Offsets” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.