Free Online XML to JSON Converter

Parse any XML document and convert it to a structured JSON object instantly. Handles attributes, nested elements, arrays, and namespaces.

Related Tools

Frequently Asked Questions

How are XML attributes handled in the JSON output?

XML attributes are mapped to a special key (typically $ or @) in the JSON object, following the xml2js convention. For example, <item id="1"> becomes { "$": { "id": "1" }, "_": "..." }.

Are XML namespaces preserved?

Yes. Namespace prefixes are preserved as part of the key names in the output JSON. Namespace declarations (xmlns) are also included in the attribute object.

What happens with repeated XML elements?

Repeated sibling elements with the same tag name are collected into a JSON array. A single element produces an object; multiple produce an array — this is standard xml2js behavior.

Converting XML to JSON: Bridging Legacy and Modern Data Formats

XML (Extensible Markup Language) and JSON (JavaScript Object Notation) are the two dominant formats for structured data interchange, but they come from different eras and embody different design philosophies. Many legacy systems, enterprise APIs, and SOAP web services output XML, while modern REST APIs and applications prefer JSON. Converting between these formats is a common task for developers integrating disparate systems, processing legacy data exports, or migrating from older infrastructure to modern services.

The Structural Differences Between XML and JSON

XML and JSON model structured data differently in ways that make perfect mechanical conversion impossible. XML supports attributes (key-value pairs within a tag) in addition to element content, while JSON has only key-value pairs. An XML element like <book id="123" title="example">content</book> contains both attributes and text content simultaneously — there is no direct JSON equivalent. XML supports namespaces that scope element and attribute names, enabling multiple vocabularies in a single document without name collisions. XML distinguishes between text nodes and element nodes within the same parent. None of these concepts have direct JSON counterparts.

JSON, conversely, natively represents arrays — ordered sequences of values — in a way that XML does not. An XML representation of a list requires either repeated elements with the same name or a wrapper element with child elements. The order of XML elements is meaningful (XML is sequence-ordered), while JSON object key order is not guaranteed in all implementations. These structural mismatches mean that XML-to-JSON conversion always involves design decisions about how to map XML's richer model into JSON's simpler one.

Common Conversion Conventions

Several conventions have emerged for handling XML features that lack direct JSON equivalents. Attributes are typically prefixed with @ to distinguish them from child elements: <book id="123"> becomes {"book": {"@id": "123"}}. Text content within elements that also have attributes is typically mapped to a special key like #text or _: <book id="123">Title</book> becomes {"book": {"@id": "123", "#text": "Title"}}. Repeated sibling elements with the same name are typically collected into a JSON array.

The challenge is that different conversion tools use different conventions, so JSON output from one XML converter may not match another converter's output even for the same input document. When integrating with a specific system, always verify which convention that system uses and configure your converter to match, or write custom conversion logic that maps the XML structure to exactly the JSON shape your application expects. Libraries like xml2js, fast-xml-parser, and xmltodict offer configuration options for controlling these conventions.

Working with XML Namespaces

XML namespaces are used extensively in enterprise and standard schemas — SOAP envelopes use namespace prefixes like soap: and xs:, XHTML uses the http://www.w3.org/1999/xhtml namespace, and SVG uses http://www.w3.org/2000/svg. When converting XML with namespaces to JSON, namespace prefixes are typically included in the key names (soap:Body becomes "soap:Body") or stripped entirely if the context makes them unnecessary. For SOAP web service responses, stripping SOAP envelope wrapper elements and namespace prefixes is usually the first step in making the data usable as JSON, since the envelope is a transport artifact rather than business data.

Namespace-aware conversion is essential when consuming industry-standard XML formats: financial data in XBRL (eXtensible Business Reporting Language), healthcare data in HL7/CDA (Clinical Document Architecture), geospatial data in GML (Geography Markup Language), or metadata in Dublin Core all rely on specific namespace definitions for their semantics. Converting these formats to JSON for processing requires understanding which namespace-qualified elements carry meaning and ensuring that meaning is preserved in the JSON representation.

SOAP Services and XML to JSON Translation

Many enterprise applications and government services still use SOAP (Simple Object Access Protocol) web services that communicate exclusively in XML. Consuming these services from modern JavaScript applications or REST-oriented microservices requires translating SOAP XML responses to JSON. API gateways like AWS API Gateway, Kong, and Azure API Management can perform this translation automatically, presenting a SOAP backend service as a REST API with JSON to modern consumers. This abstraction layer allows modern applications to consume legacy services without needing to parse XML directly.

For direct integration without an API gateway, consuming SOAP in Node.js is straightforward with the soap library or by using xml-js and a custom mapping layer. The most reliable approach for complex SOAP integrations is to write explicit mapping functions that extract known fields from the XML response rather than attempting generic XML-to-JSON conversion — this produces predictable, type-safe output and makes the integration resilient to minor XML structure changes that might confuse a generic converter.

Practical Tips for XML-JSON Conversion

When converting XML to JSON in production code, always handle the case where expected elements are missing — XML documents from external sources frequently have optional fields that appear in some responses but not others. Validate converted JSON against a schema (using Zod, Joi, or JSON Schema) to catch unexpected structural changes in the source XML before they cause runtime errors deep in your application. Log the raw XML for any conversion that fails, so you have the data needed to debug and fix the conversion logic. For high-volume conversions, benchmark your chosen library with realistic sample data — fast-xml-parser is significantly faster than xml2js and is a good default for performance-sensitive applications.