XML to JSON Converter: Parse XML Documents to JSON Online
Convert XML documents to JSON with configurable attribute handling, text node mapping, and array coercion. Perfect for consuming XML APIs in JavaScript applications.
Try the free online tool
Runs entirely in your browser — no signup, no uploads.
XML and JSON are both hierarchical data formats, but they represent that hierarchy in fundamentally different ways. XML uses elements, attributes, and text nodes with a strict tree structure, while JSON uses objects, arrays, strings, and numbers in a more flexible and JavaScript-native format. As the industry has shifted toward JSON-based REST APIs, developers regularly need to consume data from legacy XML APIs, SOAP services, or data feeds and transform it into something their JavaScript or Python code can work with naturally.
Converting XML to JSON is not straightforward because there is no single universally accepted mapping. XML attributes, text nodes, mixed content (elements with both text and child elements), and repeated sibling elements all require decisions about how they map to JSON properties and arrays. Different conversion libraries make different choices, leading to incompatible output formats. This tool gives you configurable control over the most important mapping decisions.
The converter handles attributes, nested elements, text content, CDATA sections, and repeated sibling elements (which are automatically converted to JSON arrays). The output is clean, consistently structured JSON ready to use in any modern application.
What Are the Challenges of XML to JSON Conversion?
The core challenge is that XML has more structural concepts than JSON. XML elements can have both attributes and text content simultaneously, which has no direct JSON equivalent. A common convention is to map attributes to a special key like '@attributes' and text content to a key like '#text', but other conventions exist and libraries differ on this.
Repeated sibling elements present another challenge. In XML, <item> can appear multiple times as children of a parent element, naturally representing a list. In JSON, a list is an array. But a conversion library that sees only one <item> element might output an object rather than an array, and adding a second <item> later would break the structure. This tool lets you configure forced-array paths to ensure consistency.
Mixed content (where an element has both child elements and inline text) is particularly tricky. Most conversion tools handle it by wrapping text runs in a special key. Understanding these conventions helps you write code that correctly accesses the converted data.
How to Use This Tool
Follow these steps to convert your XML to JSON:
- 1
Paste your XML document
Copy your XML content into the input panel. The tool accepts any well-formed XML document, from simple flat structures to deeply nested documents with namespaces and attributes.
- 2
Configure attribute handling
Choose how XML attributes are mapped in the JSON output. Options include ignoring attributes, merging them as properties at the same level as child elements, or grouping them under a special '@' key.
- 3
Set text node mapping
Choose how element text content is represented. When an element has both attributes and text, the text is typically placed under a '#text' key. For elements with only text content, you can opt to use the string value directly.
- 4
Configure array coercion
Specify element names that should always be output as JSON arrays, even if only one instance is present. This prevents the common bug where single-item lists are output as objects instead of one-element arrays.
- 5
Click Convert and copy
Press Convert to generate the JSON. Review the output structure, then copy it to your clipboard or download as a .json file.
Common Use Cases
XML to JSON conversion is used throughout modern software development:
- Consuming legacy SOAP web service responses and integrating them into REST-oriented JavaScript applications
- Converting RSS and Atom XML feeds into JSON for use in custom news readers, dashboards, or notification systems
- Parsing XML configuration files (like Spring or Maven) into JSON for tooling that works with JSON schemas
- Transforming XML data exports from ERP, CRM, and healthcare (HL7, FHIR XML) systems into JSON for API delivery
- Processing SVG files as structured data to programmatically manipulate paths, shapes, and transforms in JavaScript
Tips and Best Practices
Apply these tips for reliable XML to JSON conversion:
- Always specify forced-array elements for any repeating structure. If a list sometimes has one item and sometimes has many, forcing it to an array ensures your parsing code is consistent and does not need to check whether it received an object or an array.
- If you are consuming a SOAP API, strip the SOAP Envelope and Body wrapper elements first (or handle them in your code) so your application logic works with the inner payload elements directly.
- Namespace prefixes (like ns1:element) become part of the property name in JSON. If you want clean property names, either strip namespaces before conversion or apply a post-processing step to remove the prefix.
- Test your conversion with edge cases: an empty element, a single-child list, a deeply nested structure, and an element with both attributes and child elements. Make sure the output matches your expectations before writing parsing code against it.
- Consider using a typed schema (Zod, JSON Schema) to validate the converted JSON structure before your application code consumes it, especially when the XML source is external and may change.
Frequently Asked Questions
Is there a standard way to convert XML to JSON?
No universal standard exists. Several conventions have emerged, including the BadgerFish convention, the GData convention, and the Parker convention. Each makes different choices about how to handle attributes and text nodes. This tool documents which conventions it follows so you can write compatible parsing code.
How are XML namespaces handled in the JSON output?
By default, namespace prefixes are included in the property names (for example, soap:Body becomes 'soap:Body'). You can enable the 'strip namespaces' option to remove prefixes and produce cleaner property names, though this will discard namespace information.
How do repeated child elements become JSON arrays?
When the converter encounters multiple sibling elements with the same tag name, it automatically groups them into a JSON array. If only one such element exists in the document, you need to use the forced-array setting to ensure consistent array output.
What happens to XML comments during conversion?
XML comments are not data and are discarded by default during conversion. If you need to preserve comments, consider a different approach, such as representing them as a special JSON property, but note that JSON has no native comment syntax.
Can I convert very large XML files?
Browser-based tools load the entire file into memory, which works well for files up to a few megabytes. For large XML files (tens or hundreds of megabytes), use a streaming SAX-based parser in a server-side environment such as Node.js with the xml2js or fast-xml-parser libraries.
How do CDATA sections appear in the JSON output?
CDATA section content is treated the same as regular text content. The CDATA wrapper is stripped and the raw text is placed into the corresponding JSON text property. This ensures the content is accessible as a plain string in JSON.
Can I reverse the conversion and go from JSON back to XML?
Yes, though the process requires the same convention decisions in reverse. Our JSON to XML converter tool performs this transformation. Note that information not represented in JSON (like XML namespaces) cannot be round-tripped unless you chose a convention that encodes them.
Ready to use this tool?
Free, instant, no account required. Runs entirely in your browser.