How to Convert XML to JSON: What Happens to Attributes and Arrays
Convert XML to JSON online without surprises: lost attributes, single items that are not arrays, numbers as strings and mixed text, with worked examples.
The short answer
To convert XML to JSON, paste it into the XML Formatter & Validator, make sure it shows formatted XML rather than an error, then click the JSON tab and copy the output. XML and JSON do not map one-to-one, so check four things in the result: attributes are not carried over, repeated elements become arrays while single ones do not, all values are strings, and text mixed with tags is simplified.
Step by step
The conversion runs in your browser using its built-in XML parser.
A worked example
Input XML (two books in a catalogue):
<catalog><book id="1"><title>Clean Code</title><price>45.99</price></book><book id="2"><title>Refactoring</title><price>47.50</price></book></catalog>
JSON output:
{ "book": [ { "title": "Clean Code", "price": "45.99" }, { "title": "Refactoring", "price": "47.50" } ] }
The output in the tool is indented over several lines; it is shown on one line here. Notice what changed:
- The two
bookelements became an array because they share a name. id="1"andid="2"are gone.- The prices are strings in quotes, not numbers.
- The outer
catalogelement is not a key. The JSON object is its contents.
The four differences to watch
1. Attributes are not included
XML can store data as attributes (<book id="1">) or as child elements (<id>1</id>). JSON has only keys and values, so converters have to choose a convention, such as prefixing attribute names with @ or _. The Lazyblink converter keeps element names and text only and leaves attributes out.
This matters most for IDs, currency codes and units, which are often attributes. An element that has attributes but no content, such as <user id="7" active="true"/>, converts to an empty string.
Fix: if you need the attributes, rewrite them as child elements before converting. <book id="1"> becomes <book><id>1</id>...</book>. Find and Replace with a regular expression can do this across a whole file if the pattern is consistent.
2. One item is not an array, several are
When siblings share a name, the converter collects them into an array. When there is only one, you get a single value:
| XML | JSON |
|---|---|
One item inside order | { "item": "Pen" } |
Two item elements | { "item": ["Pen", "Pencil"] } |
Code that loops over item will break the first time an order has a single line. Either handle both shapes in your code, or normalise the JSON so that lists are always arrays.
3. Every value is a string
XML has no built-in difference between 45.99, true and hello; they are all text. The converter keeps them as JSON strings. Convert them in your code, for example with Number() in JavaScript or float() in Python, and be careful with values like phone numbers, PIN codes or IDs with leading zeros, which should stay as strings.
4. Mixed text and empty elements are simplified
- Mixed content. In
<p>Hello <b>world</b> again</p>, the loose text is dropped and you get{ "b": "world" }. XML that reads like a document (paragraphs with bold or links inside) is a poor fit for JSON. - Empty elements. An empty element such as
<note></note>becomes an empty string. If several empty siblings share a name with a non-empty one, the empty ones can disappear from the array, so check the count.
When this converter is the right tool
It works well for data-shaped XML: lists of records where the values are inside elements. Typical examples are product feeds, simple configuration files, RSS-style lists and API responses where attributes carry nothing important.
It is not the right tool when attributes hold essential data, when the XML uses namespaces you need to keep, or when the XML is document-style text. In those cases, use a library in your language that lets you configure how attributes and arrays are handled, and test it on your real data.
The formatter side of the tool
The same page also pretty-prints XML. Paste compact, single-line XML and the Formatted XML tab shows it indented by two spaces per level. It works best on compact XML; if your input is already indented, the original whitespace is kept and the result may look more deeply indented. If the output shows an error message instead, the message usually includes a line and column number, which points to where the parser gave up.
Checklist before you use the JSON
Summary
- Paste XML, fix any parse error, switch to the JSON tab and copy.
- Attributes are not included; convert them to elements if you need them.
- Repeated elements become arrays, single elements do not.
- All values come out as strings.
- Mixed text and empty elements are simplified, so check document-style XML carefully.
Frequently asked questions
How do I convert XML to JSON online?
Paste the XML into the XML Formatter, check that it parses without an error, then switch the output tab from Formatted XML to JSON and copy the result.
Why are my XML attributes missing from the JSON?
The Lazyblink converter keeps element names and text only. Attributes such as id="1" are not included. If you need them, turn each attribute into a child element before converting, or use a library that maps attributes to keys.
Why is one item an object but several items an array?
Repeated sibling elements with the same name become a JSON array. A single element becomes a plain value or object. Code that reads the JSON should handle both shapes, or you should wrap single items in an array afterwards.
Are numbers converted to JSON numbers?
No. XML has no data types, so every value comes out as a JSON string, for example "45.99". Convert values to numbers in your code where needed.
Put this guide into practice with our free online tool — no signup required.
Open tool