XML to JSON Converter

Convert XML documents into clean, structured JSON directly in your browser.

Looking for JSON → XML?JSON to XML Converter

Source Payload (XML)

UTF-8 · XML 1.0
10 lines·265 chars·265 B

Parsed JSON Result

Output Format
21 lines·393 B·0.00ms
Valid XML
  • Zero Server Transmission
    Your XML documents are processed exclusively in your browser memory. Data never touches remote servers.
  • Predictable Structural Mapping
    XML attributes, nested nodes, repeated sibling tags, and CDATA blocks are deterministically mapped to JSON structures.
  • Real-Time XML Validation
    Malformed syntax, unclosed tags, and character errors are pinpointed with precise line and column numbers.

Technical Foundation

Understanding XML → JSON Mapping

XML and JSON represent hierarchical data using fundamentally different tree models. ToolPorta maps XML nodes into clean, idiomatic JSON objects according to predictable rules.

Unlike XML, JSON has no native concept of attributes or duplicate object keys. ToolPorta maps XML attributes with an '@' prefix and automatically vectorizes repeated sibling tags into JSON arrays.

XML ConceptJSON RepresentationExample Mapping
Element with TextString Key/Value<name>Ali</name> → "name": "Ali"
XML Attributes@ Prefix Property<user id="12"> → "@id": "12"
Repeated ElementsJSON ArrayMultiple <tag> → "tag": ["A", "B"]
Attributes + Text@attr and #text<price cur="MAD">100</price> → "#text": "100"
NamespacesQualified Key<soap:Envelope> → "soap:Envelope": { ... }
CDATA BlocksUnescaped String<![CDATA[raw]]> → "raw"

Sample Conversion Contract

Input XML Document
<user id="42" active="true">
  <name>Yassine</name>
  <tag>developer</tag>
  <tag>morocco</tag>
</user>
Resulting JSON Object
{
  "user": {
    "@id": "42",
    "@active": "true",
    "name": "Yassine",
    "tag": [
      "developer",
      "morocco"
    ]
  }
}

Want to convert XML to JSON without losing attributes, mixed text, or array structures?

Read the Complete Guide
Code Reference

Developer Implementation Guides

Production snippets for parsing and converting XML to JSON across standard runtimes.

// Node.js & Browser via fast-xml-parser
import { XMLParser } from "fast-xml-parser";

const xmlData = '<user id="42"><name>Yassine</name></user>';

const parser = new XMLParser({
  ignoreAttributes: false,
  attributeNamePrefix: "@",
  textNodeName: "#text"
});

const jsonObj = parser.parse(xmlData);
console.log(JSON.stringify(jsonObj, null, ));

Knowledge Base

Frequently Asked Questions

Attributes are converted into object properties prefixed with an '@' character (for example, id="42" becomes "@id": "42"). If an element contains both attributes and inner text, the text is captured under the "#text" key.
When an XML element contains multiple children with the same tag name, the parser automatically combines them into a JSON array of values or objects.
By default, namespace prefixes (such as <soap:Envelope> or <xmlns:atom>) are preserved as part of the JSON object key. You can toggle namespace stripping in the Advanced Options.
No. All parsing, validation, and JSON generation runs locally in your browser using fast-xml-parser and Web APIs. No network requests are made.
XML values are inherently text. Auto-converting strings like "0123" or "1.00" into numbers can cause data loss or strip leading zeros. You can enable 'Infer numbers & booleans' in Advanced Options if you prefer typed primitives.
The parser performs strict syntax validation before parsing. If a tag is unclosed or mismatched, exact line and column numbers are reported in the toolbar. DTD and DOCTYPE declarations are explicitly rejected for security.
View all tools (15)