Skip to main content
UFOZoo

XML to JSON Converter: XML Files to JSON Instantly

XML to JSON converter online: transform XML documents and API responses into JSON with attribute and array handling for clean output.

Updated 2026-08-26

Related Tools

Features

  • Instant XML to JSON conversion: the result updates live as you type or edit
  • Attributes become keys with the @_ prefix (e.g. id="bk101" → "@_id": "bk101"), matching the fast-xml-parser convention
  • Repeated sibling elements are merged into JSON arrays automatically
  • CDATA sections are preserved as plain text values; no markup escaping needed
  • Optional type conversion: numeric text (42, 3.14) becomes JSON numbers and "true"/"false" becomes booleans
  • Empty element handling: choose whether <tag/> becomes "" or null in the output
  • Indentation selectable between 2 and 4 spaces for readability
  • Strict XML validation with exact error line and column: closing-tag mismatches, unescaped characters, and malformed structures are pinpointed
  • Mixed text and child elements are kept under a #text key instead of being silently dropped
  • Load via drag-and-drop or file picker, then save the output as a .json file or copy it in one click
  • Handles deeply nested documents: multi-level trees, namespaces (xmlns), and processing instructions
  • Works offline after first load

How to Use

  1. 1Paste XML into the left panel; the JSON conversion happens instantly, with no convert button to press.
  2. 2Drag a .xml file (for example an exported configuration or an API response) onto the editor to load it.
  3. 3Convert a file with attributes (like <book id="bk101">) and check how each attribute becomes an @_ prefixed key in the JSON output.
  4. 4Try the Book Catalog example to see repeated <book> elements become a JSON array.
  5. 5Use the 'Type conversion' toggle: on converts numeric text to numbers, off keeps everything as strings.
  6. 6Use the 'Empty nodes' toggle to decide whether <tag/> becomes "" (useful when round-tripping) or null.
  7. 7Switch the Indent option between 2 and 4 spaces to match your team's JSON style.
  8. 8When validation fails, the error line turns red in the left gutter; the panel below shows the message and position.
  9. 9Click the copy icon to grab the JSON for your JavaScript code, or Save to download converted.json.
  10. 10Use the Config File example to see how nested XML trees map to nested JSON objects.

Frequently Asked Questions

How do I convert XML to JSON?

Paste your XML into the left panel and the JSON output appears instantly on the right. The converter parses the XML tree and rebuilds it as JSON: elements become objects, repeated elements become arrays, and attributes become @_-prefixed keys. There is no convert button; output updates as you type.

Why do my XML attributes disappear in the JSON output?

They do not disappear; attributes become JSON keys prefixed with @_, following the fast-xml-parser convention. For example <book id="bk101" category="fiction"> becomes {"@_id": "bk101", "@_category": "fiction"}. This keeps attributes visually distinct from child elements. If you use tools like fast-xml-parser or xml2js to consume the result, they can read this shape directly.

What happens when the same tag appears multiple times?

Repeated sibling elements are merged into a JSON array. For example two <book> elements under <catalog> produce "catalog": {"book": [...]}. If only one element exists it stays a plain object, a common pitfall when writing code that consumes the output. Consider normalizing with a schema before use.

How is CDATA handled?

CDATA sections are kept as plain text values. The content inside <![CDATA[...]]> is extracted as-is, which is especially useful for XML that embeds HTML or code; you get the raw text without markup. There is no separate marker for CDATA in the output.

Why are XML comments missing from the JSON output?

Because JSON has no comment syntax; comments are dropped during conversion. If you need to preserve notes from the source document, keep a copy of the original XML or move important comments into a dedicated element before converting.

Why does my XML show as invalid?

Common causes: unclosed tags, mismatched closing tag names, illegal characters in text (like raw & instead of &amp;), multiple root elements, and invalid attribute quoting. The validator pinpoints the exact line and column in red; fix that position and the conversion proceeds.

Why is the XML declaration (<?xml version="1.0"?>) missing from the JSON output?

Because the declaration is metadata about encoding and version, not data; it is deliberately not included in the JSON output. Processing instructions inside the document are also skipped. Namespace declarations (xmlns attributes) are kept as regular @_-prefixed attributes.

Why does my empty <tag/> become an empty string instead of null?

Because "" is the default choice for round-tripping: an empty element like <tag/> becomes "" by default, or null if you switch the 'Empty nodes' option. If the element has attributes, they are kept and the empty value is stored under a #text key. Switch the toggle when you need JSON null values instead.

Does the converter change numbers or booleans?

Only if you enable type conversion. With it on, text matching a numeric pattern (42, 3.14, -1e5) becomes a JSON number and "true"/"false" becomes booleans. With it off, everything stays a string. XML has no native types, so the choice depends on how the JSON will be consumed; API payloads usually want numbers.

What if an element has both text and child elements?

The direct text is kept under a #text key alongside the child element keys. For example <p>Hello <b>world</b></p> becomes {"b": "world", "#text": "Hello"}. Whitespace-only text is trimmed. This avoids silently losing mixed content.

Can I convert large XML files?

Yes. Typical config files and API responses (a few MB) convert instantly. Very large files (10 MB+) may slow down the browser since everything runs locally. For production-scale parsing, tools like fast-xml-parser in Node.js are faster.

How do I convert XML to JSON in Python?

The usual path is the xmltodict library: xmltodict.parse(xml_string) returns a dict, then json.dumps() serializes it; attributes become @-prefixed keys, the same convention this tool uses. For one-off conversions, pasting the XML here and copying the JSON is faster than writing a script.

How do I convert XML to JSON in JavaScript?

Browsers can walk the XML with DOMParser, but the standard tool is the fast-xml-parser npm package (this tool follows its @_ attribute convention): new XMLParser().parse(xml) returns the object directly. For occasional use, paste the XML here and copy the result; no code needed.

How is this different from an XML formatter?

An XML formatter beautifies the XML itself (indentation, line breaks) while keeping it XML. This tool converts the document into the JSON data structure so it can be consumed by JavaScript, APIs, or NoSQL databases. Use the XML formatter when you need to keep the format.

Can I remove the root element from the JSON output?

No. The output keeps the full tree, including the root element, so the JSON always mirrors the XML document exactly. If you need the children of the root as the top level, delete the root key manually after conversion or adjust your code that reads the output.

Can I load XML from a URL?

No. This tool does not fetch remote URLs. Copy the XML content and paste it into the left panel, or open a local .xml file. This also guarantees your data is never sent to a third-party server.

Can I convert JSON back to XML?

This tool converts XML to JSON only. For the reverse direction, use a library like fast-xml-parser (XMLBuilder) or xml2js in Node.js; the @_ attribute prefix and #text keys used here map back cleanly to their XML source.

Why does my xml to json converter produce a different result than the Java library (Jackson) in my project?

XML-to-JSON has no single standard, so every library picks its own conventions: attributes may become @attr (fast-xml-parser), _attr (Jackson XML), or $attr (older tools); repeated tags become arrays in some and objects in others; text-only nodes may be a string or a {"#text": "..."} object. This tool follows the fast-xml-parser convention and documents it. If your code consumes a specific library's shape, convert a sample with both and map the differences once; the data content is the same.

Why does my xml to json converter online reject files with a DOCTYPE declaration?

DOCTYPE and external entities are blocked deliberately: parsing them can trigger XXE attacks (external entity expansion, 'billion laughs'), a well-known XML security hole, and this tool never fetches remote resources. Remove the DOCTYPE line and convert the document content itself, or process the XML server-side in a controlled environment if you genuinely need entity expansion. Legitimate XML without a DOCTYPE converts normally.