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.