XML to JSON
Convert XML to JSON.
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
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?
What happens to XML attributes?
Why are my numbers strings?
How are repeated tags handled?
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 YouTubeSQL 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.