XML to JSON Converter

Bidirectional XML and JSON converter with three mapping strategies. Choose compact for xml2js-style output, verbose for lossless round-trips, or simplified for the cleanest JavaScript-friendly result. Handles attributes, CDATA, namespaces, and repeated elements.

Conversion tool · 100% client-side · no data sent

XML Input 0 chars
Waiting for input
JSON Output 0 chars
Idle

How to Convert XML to JSON

Choose a mapping strategy

Compact (default) puts attributes under @-prefixed keys, text content under #text, and merges repeated child elements into arrays — matching the convention used by xml2js and most backend XML libraries. Verbose produces an explicit { name, attributes, children } structure for every node with no ambiguity, ideal for fully lossless round-trips. Simplified collapses text-only elements into bare string values, giving the cleanest JavaScript-friendly output at the cost of losing attributes on those leaf nodes.

Paste or type your XML

Enter any valid XML into the left pane. The tool converts live as you type when Live mode is enabled. Toggle Live off to convert only on demand by clicking Convert. Click Sample to load a realistic library catalogue example with attributes, repeated elements, and a CDATA section to explore all three strategies.

Attributes, CDATA, and namespaces

CDATA sections are preserved as text content in all three modes. Comments are dropped by default (JSON has no comment syntax) but are retained as typed nodes in verbose mode. Namespace prefixes like soap:Envelope or xsi:type are kept verbatim as part of element and attribute names — the tool does not resolve namespaces but handles them without errors. All XML entity references (&, <, numeric &#NN;) are decoded on parse and re-encoded on emit.

Reverse conversion: JSON to XML

Switch the direction toggle to JSON → XML to reconstruct XML from a JSON object. Compact JSON expects @-prefixed attribute keys and #text for text content. Verbose JSON must be an array of typed node objects. Simplified JSON treats all primitive values as child element text, with _text as the special text key. Use Swap to send the output back as input for immediate verification of the round-trip.

Frequently Asked Questions

Why convert XML to JSON?

XML is still common in legacy APIs, SOAP services, RSS feeds, configuration files, and government data formats. JSON is the native data structure for JavaScript and the dominant format for modern REST APIs. Converting makes XML data trivially consumable by any modern frontend, Node.js service, or Python script.

What is the difference between the three mapping strategies?

Compact puts attributes under @-prefixed keys, text under #text, and merges repeated child elements into arrays — matches xml2js conventions. Verbose produces an explicit { name, attributes, children } structure with no ambiguity. Simplified collapses text-only elements into bare strings, producing the cleanest JavaScript-friendly output but losing attributes on those nodes.

Does it handle CDATA, comments, and namespaces?

CDATA sections are preserved as text content. Comments are dropped by default (JSON has no comment syntax) but kept in verbose mode. Namespace prefixes like soap:Envelope are kept verbatim as part of element and attribute names — the tool does not perform namespace resolution but does not break on prefixed names either.

Is the conversion lossless?

Verbose mode is fully lossless. Compact mode loses comments and the order between attributes versus children. Simplified mode additionally loses attributes on text-only elements. The reverse direction reconstructs valid XML from any of the three formats as long as the JSON matches the chosen strategy.

Are my files uploaded anywhere?

No. The parser, serializer, and reverse converter all run entirely in your browser using JavaScript. Nothing is sent to any server, no analytics observe input, and the page works offline once loaded.