Complete Guide 5 min read

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.

convert xml to jsonxml to json onlinexml attributes to jsonxml to json arrayxml to json converter free

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

  • Open the XML Formatter & Validator.
  • Paste your XML into Input XML, or click Load sample to try it first.
  • Look at the output panel. If it shows a red error message, the XML is not well-formed and needs fixing first. The guide to fixing XML parse errors covers the usual causes, such as unclosed tags.
  • Click JSON above the output.
  • Click Copy.
  • If you want to check or tidy the JSON, paste it into the JSON Formatter.
  • 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 book elements became an array because they share a name.
    • id="1" and id="2" are gone.
    • The prices are strings in quotes, not numbers.
    • The outer catalog element 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:

    XMLJSON
    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

  • The XML parses without errors.
  • Any attributes you need were moved into elements first.
  • Lists that may contain one item are handled as arrays in your code.
  • Numeric and boolean values are converted where needed, and IDs with leading zeros stay as strings.
  • Spot-check the record count against the XML.
  • Run the JSON through a formatter or validator before sending it to another system.
  • 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.

    Try this tool on Lazyblink

    Put this guide into practice with our free online tool — no signup required.

    Open tool