Back to Tools

XML to JSON Converter

Transform XML documents into structured JSON with attribute & element handling

⚡ Nested Support 🔖 Attributes 📋 Pretty Print
XML Input

Read XML as JSON

JavaScript, REST clients and many databases handle JSON far more easily than XML. This converter parses the XML you paste, using the browser's own XML parser, and rebuilds it as a JSON object that follows the same hierarchy. A badge tells you whether the XML was accepted as valid.

Step by step

  1. Paste XML into XML Input, or press Sample to load a small store with products, prices and tags.
  2. Pick an Indentation of 2 spaces, 4 spaces or Minified.
  3. Choose Attribute Handling, described below.
  4. Tick Force Arrays if you want every child element wrapped in an array, even when it appears only once.
  5. Press Convert to JSON, then Copy or download output.json. The stats bar lists input bytes, output bytes and the number of top-level keys.

Three ways to treat attributes

  • Prefix with @ writes each attribute as a key such as @id beside the child elements.
  • Nest in _attr gathers all attributes into a single _attr object, which keeps them separate from element names.
  • Flatten (ignore) drops attributes entirely.

An element that has both text and attributes stores the text under a #text key. An element with only text becomes a plain string, and an empty element becomes null.

Repeated elements and arrays

When siblings share a name, like several product elements, they are combined into a JSON array automatically. A single product would normally appear as an object rather than a one-item array, which can break code that expects a list. Force Arrays avoids that inconsistency by always producing arrays for children.

Practical scenarios

Things to remember

The root element name becomes the top-level key of the result. All values stay as strings; numbers such as 149.99 or flags such as true are not converted. Comments and processing instructions are ignored. Invalid XML displays the parser's error in the output box with an invalid indicator.

Because the result is built from the XML you paste, you can keep pasting revised versions and converting again until the structure suits the code that will consume it.

Related Articles


Related Tools








Frequently Asked Questions

Why is one element an object and repeated ones an array?
The converter creates an array only when it finds siblings with the same tag name. A lone element stays as a single object. Turn on Force Arrays if you want consistent list output regardless of how many times an element appears.
Where do attributes go in the JSON?
That depends on Attribute Handling. Prefix with @ places keys like @id in the same object, Nest in _attr collects them under one _attr key, and Flatten discards them. Text next to attributes is stored under #text.
Are numbers and booleans converted to real JSON types?
No. Every value is kept as a string, so 42 becomes "42" and true becomes "true". If your application needs numeric types, convert them after parsing the JSON or adjust them in your code.
What happens with an empty element?
An empty tag without attributes or text is written as null in the JSON. If it has attributes, the result is an object containing only those attributes, according to the attribute mode you selected.
Does it keep XML comments and the XML declaration?
No. Only element structure, attributes and text content are converted. Comments, the XML declaration and processing instructions do not appear in the JSON output. In both cases the structure of parent and child elements stays the same as in the source document.
Support Chat
We typically reply within minutes

Hello! 👋 Before we start, please tell us your name.