Convert XML to JSON: attributes as @name or -name, repeated elements as arrays
Turn XML from an API, a feed or a config file into JSON your code can read. Switch direction to go back.
How to use it
Paste XML on the left, or open a .xml file. Choose how attributes are marked (@id or -id), and Always arrays if your code needs one shape. Copy or download the JSON. Use "Swap" to edit the JSON and turn it back into XML.
Examples
Attributes, repeated elements and text
<book id="bk101"><title>XML Guide</title><tag>xml</tag><tag>reference</tag><price currency="USD">44.95</price></book>
{
"book": {
"@id": "bk101",
"title": "XML Guide",
"tag": [
"xml",
"reference"
],
"price": {
"@currency": "USD",
"#text": "44.95"
}
}
}The two <tag> elements become an array; the price keeps its attribute, so its text goes under "#text".
Always arrays
<users><user id="1">Ada</user></users>
{
"users": [
{
"user": [
{
"@id": "1",
"#text": "Ada"
}
]
}
]
}With Always arrays on, a list of one user has the same shape as a list of many.
How XML becomes JSON
An element becomes a key. Its attributes become keys with a mark: "@id", or "-id" if you choose it. Elements repeated under one parent become an array, in order. An element with only text is that text; an empty element is "". A CDATA section becomes plain text. Text beside attributes or child elements goes under "#text". Spaces and line breaks around text are trimmed.
What JSON can’t hold
Questions
- Why is one element an array and another not?
An element that appears more than once under its parent becomes an array; one that appears once is a single value. Turn on Always arrays to make every element an array, so the shape doesn’t change when a list has one item. - Why are numbers in quotes?
XML has no numbers or booleans, only text, so the converter keeps every value as the string it was. Converting "44.95" to a number could change it (a price, a zip code with a leading zero). - Is my data uploaded?
No. Everything runs in your browser, so your data stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first. API responses, feeds and config files often hold keys or personal data, and the page warns you if it spots a key.