Convert XML to JSON when you need to inspect an XML response in a JSON-oriented tool, create a fixture, or move a simple document into a workflow that expects JSON. This converter accepts XML text in a field and returns indented JSON after parsing it in the browser. It is aimed at developers, integration testers, and learners working with element-based XML. The result is a convenient representation of element names and text, not a universal XML-to-JSON standard; confirm that the target application agrees with this converter's rules before using the output as an interchange contract.
Paste a complete XML document into “Paste your XML,” then press “Convert to JSON.” A realistic sample is <root><name>Asha</name><role>editor</role></root>; it becomes an object with name and role string properties. When a parent contains two children with the same tag, such as two <item> elements, the utility groups their values into a JSON array. Input changes clear the existing error, while conversion errors are displayed and previous output is cleared. The Clear action resets the text, result, and error so you can start a clean attempt.
The conversion uses the browser's DOMParser with the text/xml media type and checks for a parsererror element. It serializes the converted structure with readable two-space JSON indentation in an output area configured for output.json. A leaf element without child elements becomes its text content, or an empty string when there is no text; a parent with children becomes an object. Repeated child names become arrays. The implementation walks child elements, so XML attributes are not included in the resulting object, and it does not provide settings for attribute mapping, namespace handling, root omission, or text/element mixed-content modeling.
This mapping makes a straightforward document easy to inspect, but it does not preserve every XML detail. Attributes, namespace declarations, comments, processing instructions, mixed text around child nodes, and distinctions between some empty or whitespace-only content are outside the displayed conversion model. For example, <product id="7"><name>Pen</name></product> yields the child content structure, not an id property. If a downstream service relies on attributes or a specific schema, use a converter designed for that schema or add an explicit transformation rather than assuming this JSON is lossless.
For practical use, convert a small representative document first and compare the output with a hand-written expected object. Check repeated elements, whitespace, empty nodes, and any namespaces present in real data. If a parser error appears, ensure the input has one well-formed document root, properly closed tags, and escaped ampersands in text; the tool reports the browser parser message but does not repair malformed XML or validate against an XSD. Once valid JSON appears, you can use the JSON Formatter to inspect it further or JSON to YAML for a configuration-oriented representation.
The component stores both the XML input and resulting JSON in client-side state and invokes the converter in the browser component; it does not submit the document through a conversion endpoint. This is an implementation-specific observation, not an all-purpose site privacy promise. Follow your team's policy before pasting private documents or customer data. Preserve your original XML and test the generated JSON with the actual consumer, since parser acceptance alone does not establish data fidelity or business validity.
Inspect leaf values closely when converting real documents. The helper uses textContent for an element without child elements, so whitespace around content and entity references are interpreted by the browser's XML parser before JSON serialization. A parent with elements becomes an object keyed by child tag names. If a document mixes meaningful free text with child elements, this simple rule cannot preserve the entire mixed-content sequence as ordered text and elements. Such formats should be transformed with an explicit mapping that reflects their domain model.
A strong verification sample includes at least one repeated tag, one empty element, one attribute, and one namespace if those features appear in your source. The expected output should record what is preserved and what is intentionally omitted. Repeated tags are useful for arrays, while attributes are not copied by this utility; an empty leaf becomes an empty string. Write these expectations down before converting production data so reviewers can detect a shape change, especially if later XML documents add or remove a repeated element and alter a property's type.