XML to JSON

Convert XML to JSON.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste XML and get JSON keyed by element name, with the root element as the single top-level key. Repeated sibling tags are grouped into an array. Leaf values are always strings, and attributes appear as @name keys.

How to use

1
Step 1
Paste a well-formed XML document.
2
Step 2
Press Run.
3
Step 3
Copy the JSON.

How it works

The XML is read with the browser's DOMParser. Each element with child elements becomes an object, and an element without children becomes its text content. When the same tag appears more than once under one parent, the values are collected into an array in document order.

Worked example

Repeated tags become an array

Input
<user><id>1</id><name>Ada</name><tag>a</tag><tag>b</tag></user>
Output
{ "user": { "id": "1", "name": "Ada", "tag": [ "a", "b" ] } }
Walkthrough
The two <tag> elements merge into one "tag" array. The id is the string "1" because XML text is untyped.

Limitations

  • Attributes become "@name" keys, and text next to attributes goes under "#text", so <a id="1">x</a> becomes {"@id":"1","#text":"x"}.
  • Malformed XML stops with an "Invalid XML" error instead of producing output.
  • A single repeated element is not an array, so a one-item list looks like a plain value.
  • Mixed content (text next to child elements) keeps only the child elements.
  • All values are strings.

Questions

How do I convert XML to JSON?
Paste the XML and press Run. The root element becomes the top-level key.
What happens to XML attributes?
Each attribute becomes a key that starts with @, for example "@id". Text in an element that also has attributes is stored under "#text".
Why are my numbers strings?
XML has no types, so every leaf value stays a string.
How are repeated tags handled?
Two or more siblings with the same name are combined into an array.

Features

  • Repeated tags to arrays
  • Root element as top-level key
  • Pretty-printed JSON
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • TypeScript in 100 Seconds

    Fireship

    Typed JSON payloads show up constantly next to format/validate tools.

    Watch on YouTube
  • SQL Tutorial - Full Database Course

    freeCodeCamp.org

    JSON often lands next to SQL exports when debugging data pipelines.

    Watch on YouTube
  • Working with JSON

    MDN

    Parse and stringify patterns that pair with browser-side JSON tools.

  • web.dev

    Google

    Modern web guidance that pairs with encoding and tooling pages.