JSON to XML Converter

Convert JSON to XML, or XML to JSON, with attributes and repeated elements handled.

XML

Examples

Questions

How are XML attributes shown in JSON?

As keys that start with @, so <book id="1"> becomes {"book": {"@id": "1"}}. Going the other way, any @key becomes an attribute.

What happens to repeated elements?

Two or more elements with the same name become a JSON array. A single one stays a plain value, so check for both when you read the JSON.

Why are numbers strings in the JSON?

XML has no types: <price>29.99</price> is text. Guessing which text is a number would sometimes be wrong, for example with zip codes or IDs with leading zeros, so every value stays a string.

What is the root element?

If the JSON is an object with one key, that key is the root. Otherwise everything is wrapped in <root>, and items of a top-level array become <item> elements.

What happens to keys that are not valid XML names?

Characters XML does not allow in a name become _, and a name that starts with a digit gets a _ in front. So "first name" becomes <first_name> and "2fa" becomes <_2fa>.