In Python, you can convert XML to JSON with the built-in xml.etree.ElementTree module plus a small recursive helper, or with the third-party xmltodict library in a single line - and if you would rather skip the code entirely, a browser-based XML to JSON Converter performs the same conversion locally without any installation. The standard library approach gives you full control over the output shape, the third-party library gives you speed, and the browser tool gives you a no-upload option that is useful for API samples, configuration inspection, and test fixtures. All three produce valid JSON; none of them produce identical JSON, because XML and JSON have no universal one-to-one mapping.

Python Built-Ins: xml.etree.ElementTree Plus json
The standard library ships everything you need to parse XML and emit JSON, which makes xml.etree.ElementTree the default choice when you cannot install third-party packages. ElementTree.parse() returns a tree of Element objects where element.tag holds the local tag name, element.text holds the direct text content, and element.attrib is a dictionary of attributes. Because there is no automatic mapping from this tree to JSON, you write a small recursive helper that converts each Element into a plain dict and then pass the result to json.dumps().
The shape you produce is entirely your choice. A common convention is to wrap attributes in an @attributes key, store text under #text when the element also has children or attributes, and turn repeated sibling tags into a JSON array. None of that is enforced by the standard library, which is why two well-written Python scripts can produce very different JSON from the same XML. The freedom is useful for production ETL where the consumer has strict requirements, but it does mean you own the mapping and have to test it.
The Third-Party Shortcut: xmltodict
If you would rather not write the recursion yourself, xmltodict is the most widely cited single-call answer for converting XML to JSON in Python. After pip install xmltodict, a conversion is two lines: xmltodict.parse(xml_string) returns an OrderedDict that mirrors the document hierarchy, and json.dumps(dict_data, indent=2) serializes it.
By default, xmltodict uses an @ prefix for attributes (so an id attribute becomes @id) and a #text key for character data when it sits next to attributes or children. Repeated sibling elements become lists automatically. These defaults are convenient but lock you into one specific output shape, which may not match the schema your consumer expects. For a more rigorous downstream contract, you can generate a JSON Schema in Python from a JSON sample after the conversion and validate against it.
How to Convert XML to JSON Without Writing Python
When the goal is to inspect or share a single XML document quickly, installing Python and writing code is overkill. The XML to JSON Converter accepts one well-formed XML document in the browser, parses it locally with the DOMParser API, and applies a documented mapping described on the tool page itself. No document is uploaded and no network call is made, which matters for configuration files and API responses you do not want to leave your machine.
- Paste a single well-formed XML document into the input area. If your document begins with a <!DOCTYPE ...> declaration, remove that line first - the converter deliberately rejects DOCTYPE so it cannot be tricked into fetching external entities, DTDs, or remote resources.
- Pick an indentation style: compact (no whitespace), two-space, or four-space. The choice affects only how the JSON is serialized, not the underlying structure.
- Select Convert to JSON. The browser parses the document with the standard XML 1.0 rule set. If parsing fails, you see a visible parser error instead of partial output.
- Review the result using the @attributes and #text convention explained below, then copy it from the output panel.
The converter accepts up to 200,000 characters per paste, which is large enough for most API samples, configuration files, and small data migrations. Anything larger should be split or processed with a streaming parser in a version-controlled application rather than a single paste operation.
How the Converter Shapes the Output
The XML to JSON Converter applies an explicit, documented mapping rather than claiming to follow an official conversion standard - because no such universal standard exists between XML and JSON. Knowing the rules makes the output predictable.
- Root element becomes the single top-level JSON key.
- Attributes are grouped under an @attributes object on their parent element. They never appear as bare string keys at the same level as children.
- Plain text-only elements become JSON strings. Text is stored under #text only when the element also has attributes or child elements.
- Repeated sibling names become JSON arrays in document order. A single child remains a single value.
- Namespace prefixes are preserved in both element and attribute names (for example soap:Body stays soap:Body in the output).
- CDATA sections contribute their text content to #text or to the element string.
- Comments and processing instructions are not copied into the JSON output.
- Mixed content keeps direct text under #text and child elements by name, but the exact interleaving between text and children is not represented.
No values are converted to numbers, booleans, or null. This is deliberate: lexical XML text such as 007 or true must not silently change meaning when the JSON is consumed downstream. If your application needs typed values, coerce them after conversion.
Comparing Python Output With the Converter Output
The clearest way to see the difference between approaches is to look at one short XML snippet and three possible JSON results. Consider a document with an order that has two attributes, two repeated item children, and a note:
| Element or attribute | Python xmltodict | Browser converter |
|---|---|---|
| Root element | Top-level key order | Top-level key order |
| Attributes | Flattened with @ prefix, e.g. @id | Grouped under @attributes object |
| Repeated children | JSON array in source order | JSON array in source order |
| Mixed text with children | Stored under #text | Stored under #text |
| Namespace prefixes | Preserved | Preserved |
An xmltodict run produces an OrderedDict shaped like {"order": {"@id": "42", "@status": "paid", "item": ["Book", "Pen"], "note": "Gift wrap"}}. The browser converter produces the same structural shape but moves attributes into @attributes: {"order": {"@attributes": {"id": "42", "status": "paid"}, "item": ["Book", "Pen"], "note": "Gift wrap"}}. The difference is small but real, which is why you should never pipe one tool's output directly into a consumer that expects another tool's shape without a contract check.
Choosing Between Python and the Browser Tool
Pick Python when you need a custom mapping, when you are processing many files in a script, when streaming matters, or when the conversion must run as part of an automated pipeline. Pick the browser converter when you have a single document in front of you, want a consistent and inspectable shape, and would rather not write or debug a recursive helper. The two approaches cover different points on the same workflow rather than competing with each other.
Either way, compare the output contract required by your application before you commit. Another library may always emit arrays for children, preserve comments, coerce numbers and booleans, or split namespaces into their own object - and those differences will quietly break a consumer that expects the shape produced by the tool or library you chose.