XML and JSON are the two most common data-interchange formats, and sooner or later you'll need to convert one into the other: an older API returns XML while the frontend expects JSON, or the other way around. Let's look at how that conversion works and what to watch for.
How XML and JSON differ
XML (eXtensible Markup Language) is a tag-based format familiar from older SOAP APIs, config files, and RSS feeds. Elements can have attributes, nested tags, and text content all at once. JSON (JavaScript Object Notation) is a more compact key-value format that the vast majority of REST APIs use today.
XML is more verbose and requires closing tags, but it supports attributes and mixed content. JSON is shorter and maps more directly onto the data structures of programming languages, which is why modern APIs almost always choose it.
How XML attributes end up in JSON
When converting XML to JSON, the main question is what to do with attributes, since JSON has no equivalent of a tag attribute. They're usually turned into regular keys with a prefix, commonly @_: <item id="1">text</item> becomes {"item": {"@_id": "1", "#text": "text"}}. The prefix keeps an attribute distinct from a child element with the same name.
Repeated tags become an array
If a parent element contains several tags with the same name, converting to JSON collects them into an array:
<items>
<item>a</item>
<item>b</item>
</items>
becomes:
{ "items": { "item": ["a", "b"] } }
A single tag with no siblings, on the other hand, stays a plain object rather than a one-element array — worth keeping in mind in any code that reads the result, since a single element and a one-item array are handled differently.
How to convert online
Open the XML ⇄ JSON converter, paste in XML, and get back JSON with attributes and arrays following the rules above. Processing happens right in the browser, nothing is sent to a server, so it's safe to paste in data with real names and addresses.
Converting back: JSON to XML
The reverse direction doesn't work for just any JSON: XML always has a single root element, so JSON being converted to XML needs to be an object with one top-level key, not an array or a primitive value. If the structure came from converting XML in the first place (with @_... and #text keys), converting back restores the original attributes and text nodes.
When this comes up
- Integrating with an older API that returns XML while the rest of the system works in JSON.
- Debugging — quickly reading a long XML document in a more compact, familiar form.
- Migrating configs between systems that use different formats.
If you need to convert YAML or CSV instead of XML, there are dedicated tools for that: JSON ⇄ YAML and JSON ⇄ CSV.