XML to JSON Conversion Guide
Converting XML to JSON requires careful handling of attributes, text content, repeated elements, and CDATA sections. This guide covers the rules and common conventions.
JSON ↔ XML Converter
Convert between JSON and XML formats. Handles nested objects, arrays, attributes, CDATA sections, and XML declarations.
About JSON ↔ XML Conversion
- JSON (JavaScript Object Notation) — lightweight data format with objects, arrays, strings, numbers, booleans, and null.
- XML (Extensible Markup Language) — hierarchical markup format with elements, attributes, namespaces, and CDATA sections.
- JSON → XML: Objects become elements, arrays wrap items in a configurable tag (default: "item"), primitives become text content. Root element name is configurable.
- XML → JSON: Elements become keys, repeated sibling elements become arrays, attributes use a configurable prefix (default: "@"), text content uses a configurable key (default: "#text").
- Swap button carries output to input for round-trip conversion.
- Everything runs in your browser — no data is sent over the network.
XML attributes in JSON
XML attributes have no direct JSON equivalent. The most common convention uses a prefix character (typically '@') to distinguish attributes from child elements. For example, <book id="123"> becomes {"@id": 123}. Other conventions include a nested "_attributes" object or flattening attributes as regular keys. DevBolt's converter uses a configurable prefix (default: '@').
<!-- XML input -->
<product id="P001" category="electronics">
<name>Wireless Mouse</name>
<price currency="USD">29.99</price>
</product>
// JSON output (with @ prefix)
{
"@id": "P001",
"@category": "electronics",
"name": "Wireless Mouse",
"price": {
"@currency": "USD",
"#text": 29.99
}
}Handling repeated elements as arrays
When multiple sibling elements share the same tag name, they are grouped into a JSON array. A single element becomes a plain value. This can cause inconsistency — one <item> produces a value, two <item> elements produce an array. Some converters offer a 'force array' option for specific tags to ensure consistent array output regardless of element count.
CDATA and mixed content
CDATA sections (<![CDATA[...]]>) contain text that should not be parsed as XML markup. In JSON conversion, CDATA content is extracted as plain text. Mixed content (elements containing both text and child elements) uses a text key (default: '#text') to preserve the text alongside child element keys.
Frequently Asked Questions
How are XML attributes represented in JSON?
The most common convention prefixes attribute names with '@' — e.g., <book id="1"> becomes {"@id": 1}. This distinguishes attributes from child elements in the same JSON object. The prefix character is configurable.
Is XML to JSON conversion lossless?
Not always. XML features like processing instructions, comments, namespace declarations, document type definitions, and attribute ordering have no JSON equivalent. Element order within objects may also change since JSON objects are unordered. For most data interchange scenarios, the conversion preserves all meaningful content.
Related Convert Tools
CSS Unit Converter
Convert between px, rem, em, pt, vw, vh, and % — single values or batch-convert entire CSS files with configurable base font size
Base64 Codec
Encode and decode Base64 strings with Unicode support
Color Converter
Convert colors between HEX, RGB, and HSL formats
Epoch Converter
Convert Unix timestamps to dates and back