Blog
Wild & Free Tools

XML Attributes and Nested Elements to JSON: Full Guide

Last updated: March 2026 6 min read
Quick Answer

Table of Contents

  1. Basic Element to JSON Key Mapping
  2. XML Attributes: The @ Convention
  3. Repeated Elements Become Arrays
  4. CDATA Sections
  5. Empty Elements and Null Values
  6. Frequently Asked Questions

The way XML structure maps to JSON isn't always obvious — especially for attributes, repeated elements, and mixed content. This guide explains the exact mapping rules used by the Whale XML to JSON converter with examples for each case: attributes, nested elements, arrays, CDATA, namespaces, and mixed text content. If you want to test any example, paste the XML into the free browser converter and see the JSON output live.

Basic XML Elements to JSON Keys

The simplest conversion: an XML element with text content becomes a JSON key-value pair.

XML input:

<name>Alice</name>

JSON output:

"name": "Alice"

Nested elements become nested JSON objects:

XML: <person><name>Alice</name><age>30</age></person>

JSON: "person": { "name": "Alice", "age": "30" }

Note that all values are strings in the JSON output by default — "30" is a string, not a number. If you need typed values (numbers, booleans), you'll need a post-processing step to re-type them.

XML Attributes: The @ Prefix Convention

XML attributes are properties of an element: <product id="123" category="electronics">Widget</product>.

In the JSON output, attributes are mapped to keys with an @ prefix — the most widely-used convention for attribute-to-JSON mapping:

"product": { "@id": "123", "@category": "electronics", "#text": "Widget" }

When an element has both attributes and text content, a special #text key holds the element's text value, while the @-prefixed keys hold the attributes.

When an element has attributes and child elements (not text content), the attributes map to @-prefixed keys alongside the child element keys as siblings in the JSON object.

Sell Custom Apparel — We Handle Printing & Free Shipping

Repeated XML Elements Map to JSON Arrays

When multiple sibling elements share the same tag name, they map to a JSON array:

XML:

<items><item>Apple</item><item>Banana</item><item>Cherry</item></items>

JSON:

"items": { "item": ["Apple", "Banana", "Cherry"] }

This is how XML lists and collections translate to JSON arrays. Each repeated element becomes a member of the array. If objects (not just text) are repeated, the array contains JSON objects with their own key-value structure.

Edge case: if only one <item> exists, it maps to a single value (not an array). This inconsistency is a known challenge in XML-to-JSON conversion — some converters always produce an array for list elements, others only wrap in an array when multiple are present. The browser converter wraps in arrays only when multiple siblings are detected.

CDATA Sections in XML to JSON

CDATA (Character Data) sections in XML allow characters like <, >, and & to appear as literal text without being treated as XML markup.

XML: <description><![CDATA[Price < $100 & in stock]]></description>

JSON: "description": "Price < $100 & in stock"

The CDATA wrapper is stripped; the content becomes a plain string in the JSON output. Special characters within the CDATA section are preserved literally. This means HTML content embedded in CDATA sections (a common pattern in XML-formatted RSS feeds and content APIs) comes through as a string containing HTML markup.

Empty XML Elements and Null Values in JSON

Self-closing elements (<field/>) and empty elements (<field></field>) map to empty strings or null in the JSON output, depending on the converter.

In the Whale converter: <field/> maps to "field": null. This is a convention choice — other converters may use an empty string "" or an empty object {}. The null convention is most useful when the JSON is consumed by code that needs to distinguish between a missing field and an explicitly empty one.

If your output needs empty string instead of null, a single post-processing pass on the JSON output can substitute the values.

Test Your XML Conversion

Paste any XML and see exactly how it maps to JSON. Free, instant, no upload.

Open Free XML to JSON Converter

Frequently Asked Questions

Why does the converter use @ for attributes instead of another convention?

The @ prefix is the most widely-used convention for XML attribute-to-JSON mapping, used by libraries in Python (xmltodict), JavaScript, and Java. It avoids collisions with element names that start with letters.

What if an element name conflicts with an attribute name after adding @?

Conflicts are rare in practice since XML element names and attribute names occupy different namespaces. If a conflict does exist, most converters favor the element name and append a suffix to the attribute key.

Does the converter handle XML with mixed content (text and elements)?

Mixed content (an element containing both text and child elements) maps to an object with a #text key for the text portion and regular keys for child elements. Results can be complex for heavily mixed-content XML.

Why are my numbers and booleans strings in the JSON output?

XML has no native type system for numbers or booleans — all values are strings. The converter outputs them as strings. Add a type-casting step in your code if you need typed values.

Andrew Walsh
Andrew Walsh Developer Tools & API Writer

Andrew worked as a developer advocate at two SaaS startups writing API documentation used by thousands of engineers.

More articles by Andrew →
Launch Your Own Clothing Brand — No Inventory, No Risk