---
title: "How to convert XML to JSON | ToolSugar"
description: "Convert XML to JSON and learn how attributes, repeated elements and text are mapped, so your code reads the result correctly."
canonical: https://toolsugar.com/blog/how-to-convert-xml-to-json
image: https://toolsugar.com/og.png
---

# How to convert XML to JSON without losing attributes

Updated 2026-11-09

To convert XML to JSON, map each element to a key, each attribute to a prefixed key, and repeated elements to an array. XML to JSON on ToolSugar applies those rules in your browser with fast-xml-parser. It checks the XML first and reports the line and column of any problem.

[Open XML to JSON](https://toolsugar.com/tools/xml-to-json)

## Convert XML to JSON in four steps

Paste XML or open a .xml file. The mapping rules are shown above the box.

1. Paste XML into the XML box, or open a .xml file.
2. Read the JSON on the right and the mapping rules above the box.
3. If the XML is invalid, fix the line and column in the red message.
4. Copy the JSON, or press Download to save data.json.

## How attributes and text are mapped

An attribute becomes a key with an @\_ prefix, so id="1" turns into "@\_id": "1". That keeps attributes apart from child elements that share a name.

When an element has attributes and text, the text goes under the key "#text". An element with only text becomes a plain value.

## Why one item is not an array

Siblings with the same name become a JSON array. A single element stays a plain object, because JSON has no schema that says a field may repeat.

If a feed can hold one entry or many, write your code to accept both shapes. An RSS file with one item looks different from one with ten.

## Values, comments and the declaration

Every value stays a string, so 007 and 1.50 come through as written. The XML declaration is dropped and comments are ignored.

Because the XML is validated first, this also works as a quick well-formed check. An unclosed tag stops the conversion and points to its position.

## Questions

### Can I read a SOAP response with this?

Yes. Paste the response body and the elements and attributes appear as JSON keys, which is easier to scan than the raw tags.

### How do I make a single element always an array?

The tool cannot know that in advance. In your code, check whether the value is an array and wrap it in one when it is not.

### Are numbers converted to JSON numbers?

No. They stay strings, so leading zeros and trailing decimals are not lost.

[Open XML to JSON](https://toolsugar.com/tools/xml-to-json)

## Related guides

- [How to convert JSON to YAML](https://toolsugar.com/blog/how-to-convert-json-to-yaml)
- [How to convert YAML to JSON](https://toolsugar.com/blog/how-to-convert-yaml-to-json)
- [How to format JSON online](https://toolsugar.com/blog/how-to-format-json)
- [How to convert HTML to Markdown](https://toolsugar.com/blog/how-to-convert-html-to-markdown)
