# How to Convert XML to JSON

> Parse XML documents and transform them into clean JSON objects for use with modern APIs and JavaScript applications.

- URL: https://www.browserutils.dev/how-to/convert-xml-to-json
- Published: 2026-08-26
- Updated: 2026-07-02

---

## Step 1: Paste your XML

Copy well-formed XML content and paste it into the input editor. The tool accepts XML with attributes, namespaces, and nested elements.

## Step 2: View JSON output

The converter parses the XML and generates a structured JSON representation, mapping elements to objects and repeated elements to arrays.

## Step 3: Adjust conversion settings

Configure how attributes are handled, whether text content uses a special key, and how arrays are detected from repeated elements.

## Step 4: Copy the JSON

Click the copy button to copy the JSON output, ready for use in your JavaScript application, REST API, or data pipeline.

Modern web applications and REST APIs work with JSON, but data from enterprise systems, SOAP services, and document formats often arrives as XML. The [XML to JSON Converter](/tools/xml-to-json) transforms XML documents into clean JSON structures so you can work with the data in any JavaScript-based environment or modern API.

## Understanding XML-to-JSON conversion

XML and JSON have different data models, so conversion requires mapping decisions. XML elements become JSON object keys. Element text content becomes string values. Repeated sibling elements with the same name become JSON arrays. Attributes can be mapped to special keys (commonly prefixed with `@`) or merged into the element's object.

The ambiguity arises because XML does not distinguish between "an element that appears once" and "an array with one item." If your XML has a `<user>` element that sometimes appears once and sometimes appears multiple times, the converter needs a consistent strategy. Most converters treat repeated elements as arrays and single elements as plain values, but this can cause issues if your code expects a consistent type.

Namespaces and prefixed elements add another layer of complexity. A tag like `<ns:name>` might become `"ns:name"` or just `"name"` in JSON depending on the converter's settings. Check which behavior your downstream code expects.

## Tips and best practices

- **Ensure your XML is well-formed.** The converter requires valid XML with properly closed tags and correct nesting. Run it through an XML validator first if you are unsure.
- **Handle attributes consistently.** Decide upfront whether XML attributes should become `"@attr"` keys in JSON or be discarded. Keep this consistent across your application to avoid bugs.
- **Watch for type information loss.** XML treats everything as text. The number `42` and the string `"42"` look identical in XML. The converter may or may not attempt type inference — check the output types match your expectations.
- **Test with arrays of varying length.** If an element sometimes appears once and sometimes repeats, verify the converter produces arrays consistently. Inconsistent typing is a common source of bugs.

## Common issues

- **Single elements not converted to arrays.** If your code expects `"items"` to always be an array but the XML only has one `<item>`, the converter may produce an object instead. Force array conversion for elements you know can repeat.
- **Namespace prefixes cluttering keys.** XML namespaces like `xmlns:xsi` produce verbose JSON keys. Strip namespace prefixes if your application does not need them.
- **CDATA sections and mixed content.** XML supports CDATA blocks and mixed element/text content, which have no clean JSON equivalent. These are typically converted to plain strings, potentially losing structural information.