Tutorial · 5 min read

How to Convert XML to JSON with Attributes and Arrays

XML is still everywhere — legacy APIs, configuration files, SOAP services, RSS feeds. But modern applications speak JSON. Converting XML to JSON is straightforward in theory, but the devil is in the details: XML attributes, repeated elements, mixed content. This guide covers all of them using the XML to JSON Converter — a zero-dependency tool that runs entirely in your browser.

Why XML to JSON Is Not a Simple Find-and-Replace

XML and JSON share the same tree structure, but XML has features JSON doesn't:

A typical XML snippet shows these challenges:

<catalog>
  <book id="b001" lang="en">
    <title>XML for Beginners</title>
    <author>John Doe</author>
  </book>
  <book id="b002" lang="en">
    <title>Advanced XML</title>
    <author>Jane Smith</author>
  </book>
</catalog>

A good XML to JSON tool handles all of this automatically with the right configuration.

1. Configuring the Attribute Prefix

By default, XML attributes are prefixed with @ in the JSON output. You can customize this in the configuration bar:

{
  "catalog": {
    "book": [
      {
        "@id": "b001",
        "@lang": "en",
        "title": "XML for Beginners",
        "author": "John Doe"
      },
      {
        "@id": "b002",
        "@lang": "en",
        "title": "Advanced XML",
        "author": "Jane Smith"
      }
    ]
  }
}

The @ prefix clearly distinguishes attributes from child elements. Change it to _, -, or any other string in the config bar. The preview updates instantly so you can verify the output.

2. Automatic Array Detection

The Detect Arrays option (enabled by default) groups repeated sibling elements with the same tag name into JSON arrays. In the example above, the two <book> elements became a JSON array "book": [...].

Without this option, only the last occurrence would be kept — which is almost never what you want. Keep array detection on unless you have a specific reason to turn it off, such as when handling single-element XML structures.

Toggle the checkbox to see the difference in real time. The converter handles nested arrays with multiple levels of depth automatically.

3. Handling Mixed Content and Text Nodes

Elements with both text and child elements get a special treatment:

<description>
  This book covers
  <highlight>practical examples</highlight>
  and theory.
</description>

Converts to:

{
  "description": {
    "#text": "This book covers and theory.",
    "highlight": "practical examples"
  }
}

The text content is stored under #text while child elements keep their own keys. This preserves every piece of information from the original XML.

Try the Free XML to JSON Converter

Paste XML, configure attribute prefix and arrays, see JSON live. Zero dependencies, browser-native parsing.

Convert XML to JSON Now →

Best Practices for XML to JSON Conversion

Frequently Asked Questions

How do I convert XML to JSON online?

Paste your XML into the XML to JSON Converter. The tool parses it using the browser's native DOMParser and converts it to JSON in real time. Configure attribute prefix and array detection in the config bar, then download the result.

How are XML attributes converted to JSON?

XML attributes are prefixed with @ by default. For example, <item id="123"> becomes {"@id": "123"}. You can change the prefix to any string (e.g., _ or -) in the configuration bar.

Does the converter detect arrays in XML?

Yes. When enabled, repeated sibling elements with the same tag name are automatically grouped into a JSON array. This is enabled by default and should stay on for most XML documents.

Can I edit the XML before converting?

Yes. The XML input panel is a full text editor. Make changes, and the JSON output updates instantly. You can also beautify or minify the JSON output using the toolbar buttons.

Is my XML data safe with this converter?

100% safe. The conversion uses the browser's native DOMParser API — no data is sent to any server. Zero third-party dependencies means fewer security concerns. Your data never leaves your computer.