An XML JSON converter turns one well-formed XML document into a predictable JSON representation inside the browser, without uploading the source or processing it on a server.

XML and JSON have no universal one-to-one mapping, so a successful conversion is only the first step. The JSON object shape must also fit the application that will consume it. The XML to JSON Converter uses an explicit convention for attributes, text, repeated elements, and namespaces, making the result predictable enough to review before use.

xml json converter
XML JSON Converter: Mapping Rules and Workflow

How the XML-to-JSON Mapping Works

The root element becomes a top-level JSON key. Child element names become property names, while attributes are collected in a separate @attributes object. Text-only elements become strings. This is an explicit product convention rather than an official XML-to-JSON standard, so a receiving application may expect a different shape.

XML feature JSON representation Review point
Root element Its name becomes the top-level key Confirm the key matches the consumer's expectation
Element attributes Grouped in an @attributes object Attributes remain separate from element text
Text-only element The element value is a string No number, boolean, or null conversion occurs
Text in an element with attributes or children Stored under #text Check the property name used for direct text
Repeated sibling names Combined into an array in document order Confirm the receiving system accepts arrays
A single matching child Remains a single value rather than an array One occurrence and multiple occurrences have different shapes
Namespace prefixes Retained in element and attribute names Do not assume a separate namespace metadata model
Empty element An empty string without attributes; attributes remain when present Do not expect both cases to be plain strings
CDATA Contributes text content The text follows the same string or #text rule
Comments and processing instructions Not copied into JSON Those nodes are unavailable downstream

Type handling is deliberately conservative. No value is automatically converted to a number, boolean, or null; even text that looks numeric remains a string. This prevents leading-zero identifiers and other lexical values from silently changing meaning during conversion.

Convert XML to JSON in the Browser

  1. Prepare and paste one well-formed XML document. The input can contain up to 200,000 characters. Remove any DOCTYPE declaration before converting because declarations are deliberately blocked. Use one document rather than combining a batch into a single input. Well-formedness follows the XML syntax rules documented in the W3C XML 1.0 specification. If the XML is malformed, the converter rejects it visibly instead of returning partial JSON.
  2. Choose the JSON indentation. Select compact, two-space, or four-space indentation to control the serialized layout of the result.
  3. Convert, inspect, and copy. Select Convert to JSON, then review the generated object before copying it. Check the root key, whether attributes landed under @attributes, whether text appears directly or under #text, and whether repeated siblings became arrays. Copy the result once it matches the contract required by your application.

Parsing happens through the browser's native XML DOMParser. The converter performs no network fetches for schemas, DTDs, external entities, namespaces, stylesheets, or remote resources. The MDN DOMParser documentation provides more information about the browser API used for XML syntax parsing.

A Complete Mapping Example

Input XML: <order id="A17"><status>paid</status><item sku="X1">2</item><item sku="X2">3</item></order>

Compact JSON result: {"order":{"@attributes":{"id":"A17"},"status":"paid","item":[{"@attributes":{"sku":"X1"},"#text":"2"},{"@attributes":{"sku":"X2"},"#text":"3"}]}}

The order element becomes the top-level key, while its id attribute is grouped under @attributes. The status element has text only, so it becomes the string "paid". Because item occurs twice under the same parent, those siblings become an array in source order. Each item has both an attribute and text, so its text appears under #text. The values 2 and 3 remain JSON strings because XML text is not automatically typed. If the input had only one item, that property would remain a single object rather than becoming an array.

Special XML Inputs and Their Limits

Several XML features affect whether conversion can finish or what information survives in the JSON.

Input situation Converter behavior Practical consequence
DOCTYPE declaration Blocked rather than processed Remove it before pasting; DTD-backed resolution is outside this workflow
Malformed XML Parser errors are rejected visibly, with no partial output Correct the syntax and run the conversion again
Comments and processing instructions Omitted from the JSON representation Do not depend on these nodes in downstream code
CDATA sections Contribute text content The text appears directly or under #text, depending on the element's structure
Namespace-prefixed names Prefixes remain part of element and attribute names Do not assume namespace URIs are separated into a different field
External resources No schemas, DTDs, entities, stylesheets, or remote resources are fetched The conversion does not depend on network-loaded material

These are explicit operating limits rather than optional warnings. A malformed document or one that still contains a DOCTYPE declaration cannot complete the workflow, while comments and processing instructions are deliberately absent from the result.

Mixed Content and Consumer Expectations

Mixed content occurs when an element contains both character data and child elements. The converter combines the direct text under #text and retains the child elements by name. It does not represent the exact interleaving between text runs and child nodes, so a structure that alternates between words and inline elements cannot be reconstructed precisely from the JSON.

Before integrating the output, compare it with the receiving application's required contract. Another XML library may use different prefixes, force every child collection into an array, preserve comments, coerce numeric or boolean text, or represent namespaces separately. The generated JSON is suitable only when the application can accept this explicit convention.

When Browser Conversion Is the Right Choice

This workflow fits API samples, configuration inspection, test fixtures, and small data migrations where the resulting object shape matches the receiving system. The one-document limit, predictable mapping, and selectable indentation make it practical for reviewing and copying an individual conversion.

For very large documents, streaming conversion, schema validation, lossless mixed-content preservation, or production ETL, use a dedicated parser in a version-controlled application. That approach allows the mapping rules and tests to remain part of the codebase instead of relying on an ad hoc browser conversion.

A Review Checklist for Integration

Before copying the JSON into an application, check the following points against the expected input and output contract:

  • The root element name is the intended top-level JSON key.
  • Attributes are under @attributes rather than mixed into text content.
  • Text-only elements are strings, while structured text appears under #text.
  • Repeated sibling names are arrays in document order, and a lone child is not unexpectedly wrapped in one.
  • Namespace-prefixed names remain in the form expected by the consumer.
  • Numeric, boolean, and null-looking text remains a string where the XML requires lexical text.
  • The workflow does not depend on comments, processing instructions, fetched resources, or exact mixed-content ordering.

If the integration depends on information the converter intentionally omits or approximates, choose a different parser or transformation. Otherwise, the reviewed JSON is ready to copy into the consuming workflow.