Skip to content
PithLab

↑↓ to move · Enter to open · Esc to close

XML to JSON Converter

Turn XML documents into JSON objects, attributes included.

Use via API

Convert XML to JSON. Send JSON, get JSON back.

POST https://pithlab.com/api/v1/tools/xml-to-json

curl https://pithlab.com/api/v1/tools/xml-to-json \
  -H "Authorization: Bearer $PITHLAB_KEY" \
  -H "Content-Type: application/json" \
  -d '{"input":"<book id=\"1\"><title>Dune</title></book>"}'

Fields and responseOpenAPI documentGet an API key

About the XML to JSON

Convert XML to JSON in your browser. Keeps attributes with a prefix you choose, groups repeated elements into arrays and reports XML errors by line and column.

How to use the XML to JSON

  1. Paste your XML, or click Example.
  2. Choose the attribute prefix (default "@_"), or untick Include attributes to drop them.
  3. Tick Convert numbers and booleans to turn text such as 42 or true into JSON values.
  4. Fix any error shown using its line and column, then copy or download the JSON.

Examples

Element with attributes

Input

<book id="42"><title>Dune</title><year>1965</year></book>

Output

{
  "book": {
    "title": "Dune",
    "year": "1965",
    "@_id": "42"
  }
}
Repeated elements become an array (minified)

Input

<list><item>a</item><item>b</item></list>

Output

{"list":{"item":["a","b"]}}

Frequently asked questions

How are attributes represented?

Each attribute becomes a key with a prefix, "@_" by default, so <book id="42"> gives "@_id": "42". The prefix stops attributes clashing with child elements of the same name. You can change it or leave it empty.

Why is a single child an object but two children an array?

XML has no arrays, so repeated elements with the same name are grouped into an array. An element that appears only once stays a single value. Code that reads the JSON should allow for both.

What happens to text mixed with child elements?

Text next to child elements is stored under "#text". Whitespace around text is trimmed, so mixed content such as paragraphs of HTML may lose some spacing.

Are numbers converted?

Not by default, because XML text is always a string and values like postcodes can lose leading zeros. Tick Convert numbers and booleans to convert them. Values such as 007 still stay as text.

Are comments, the XML declaration and DOCTYPE kept?

No. The XML declaration, processing instructions and comments are left out of the JSON, as they are not part of the data.