Choose language

XML to JSON Converter

Convert XML documents into clean JSON. Map elements, attributes, and nested nodes into structured objects you can read, format, and use in code.

Mehmet Demiray Published Updated
Share
Skip XML attributes instead of including them
Spacing used to format the JSON output

Turning XML Into JSON

XML and JSON both describe structured data, but they use very different shapes. XML wraps every value in opening and closing tags and can attach attributes to those tags. JSON uses keys, values, arrays, and nested objects with far less ceremony. A converter reads the XML tree and produces the equivalent JSON structure, so you can work with the data in tools and languages that expect JSON.

A single element maps cleanly:

XML JSON
<name>Ada</name> { "name": "Ada" }

The converter walks each node from the root down, turns element names into keys, and nests child elements as nested objects so the depth of the original tree is preserved. Values become strings, since XML has no separate notion of numbers or booleans, which is worth remembering when you read the output. Once you have JSON, JSON Formatter gives it tidy indentation, and JSON to XML Converter reverses the process when you need XML again. If your source were a spreadsheet instead, CSV to JSON Converter would be the right starting point.

How Elements and Attributes Map

The trickiest part of converting XML is that it has two ways to hold data: child elements and attributes. JSON has only keys, so a converter needs a consistent rule for both.

XML feature Typical JSON form
Child element nested key
Attribute key prefixed with @
Text inside tags #text key

For example <user id="5">Ada</user> commonly becomes { "user": { "@id": "5", "#text": "Ada" } }. The @ prefix keeps attributes from colliding with child element names, and #text holds the value that sits between the tags. This convention is widely used so that no information from the original document is lost in translation. Knowing the rule helps you read the output: anything starting with @ came from an attribute, and a #text key holds the inner content of a mixed element. When an element has only text and no attributes, the converter usually simplifies it to a plain key and value, dropping the #text wrapper so the JSON stays clean and easy to consume in code.

Handling Repeated Elements as Arrays

XML often repeats the same tag to represent a list, but a single occurrence and multiple occurrences look structurally different. A converter has to decide when repeated elements become a JSON array.

Consider a list of items:

XML JSON
<item>a</item><item>b</item> { "item": ["a", "b"] }
<item>a</item> { "item": "a" }

When the same element name appears more than once under a parent, the values collapse into an array. When it appears once, it stays a single value rather than a one-item array. This can be surprising if your code always expects an array, so it is worth checking the output when a list might contain only one entry. The safest habit is to normalize in code, wrapping a value in an array if it is not already one, so a list of one and a list of many behave the same. After conversion you can open the result in JSON Formatter to confirm whether a field came out as an array or a single value before wiring it into your application.

Common XML Conversion Pitfalls

A few XML features do not have a clean JSON equivalent, and knowing them in advance saves confusion. Namespaces, like <ns:title>, often keep the prefix in the key, which can produce keys with colons that need bracket access in JavaScript. Comments and processing instructions are usually dropped because JSON has no place for them.

Mixed content, where text and child elements share the same parent, is awkward because JSON cannot interleave a string and nested objects the way XML tags can. The converter typically separates the text into a #text key and lists the children alongside it. Empty elements such as <tag/> may become null, an empty string, or an empty object depending on the rule applied. Whitespace between tags is normally ignored so it does not show up as stray text values. Reviewing the JSON output against the source XML is the surest way to confirm nothing important was reshaped unexpectedly.

When XML to JSON Conversion Helps

Plenty of older systems, enterprise APIs, and configuration formats still speak XML, while most modern front-end and JavaScript code prefers JSON. Converting bridges that gap. If you receive an XML feed from a legacy service, turning it into JSON lets you handle it with the same parsing your other endpoints use.

Conversion also helps during data exploration. A dense XML document with deeply nested tags is hard to skim, but the JSON version opens cleanly in JSON Formatter where indentation makes the structure obvious at a glance. It is also handy when feeding XML data into a JavaScript front end, since the browser works with JSON natively and needs no extra parser. When you later need to send data back to an XML-only system, JSON to XML Converter converts your JSON the other way. For spreadsheet-style sources, the CSV converter covers that case. The goal is always the same: keep the data in whatever shape the next step expects, without retyping it by hand or losing fields along the way.

The ones we answer the most.

How are XML attributes represented in JSON?

Attributes are usually mapped to keys with an @ prefix, so <user id="5"> becomes a key like @id. The prefix keeps attribute names from clashing with child element names. The text between tags is commonly stored under a #text key when an element also has attributes.

Why did a single element not become an array?

Repeated elements with the same name collapse into a JSON array, but a single occurrence stays a plain value. If your code always expects an array, a list with only one entry can surprise you. Check the output and normalize in code if you need a consistent array shape.

What happens to XML comments and namespaces?

Comments and processing instructions are normally dropped because JSON has no place for them. Namespace prefixes like ns: are usually kept in the key, which can produce keys containing a colon. Review the output if your source relies heavily on namespaces.

How are empty XML elements converted?

An empty element such as <tag/> may become null, an empty string, or an empty object depending on the conversion rule. If empties matter to your logic, inspect the JSON to confirm which form was produced before relying on it.

Does the conversion keep all my data?

Element text, attributes, and nesting are preserved using the @ and #text conventions. Some XML-only features like comments and exact whitespace are not carried over because JSON cannot represent them. Comparing the JSON against the source is the best way to confirm nothing important changed.

Can I convert the JSON back to XML?

Yes. Once you have JSON, the JSON to XML Converter tool reverses the process and rebuilds XML markup. This is useful when a downstream system only accepts XML but you prefer to work with JSON in between.