Convert XML to JSON Online

Convert XML to JSON in your browser: paste the XML on the left, press Convert, and read the JSON on the right. Elements, attributes, text, comments, CDATA, DOCTYPE, XML declarations and processing instructions all carry over into the JSON. The converter generates accurate and reversible JSON strings; you can convert XML to JSON and then convert it back from JSON to XML to its original form. The conversion runs in the page — nothing is sent to the server unless you press Save or Share.
Updated:

XML to JSON converter

An online XML to JSON converter turns an XML document into the equivalent JSON object, so you do not have to write a parser or install a library. Paste the XML, press Convert, and read the JSON on the right: the same elements, attributes and text, with the XML-only parts kept under _-prefixed keys.

  • Parse XML elements, attributes, text and comments.
  • Process CDATA, DOCTYPE, XML declarations and processing instructions.
  • Repeated tags of the same name become one JSON array, in document order.
  • Works in your browser: the conversion runs in the page, not on the server.
  • Share and discuss your XML files and conversion results online.
  • Generate PHP, Python, JavaScript, Java and Curl code for the XML you pasted.
  • Format and beautify the XML and the resulting JSON.
  • Built-in XML and JSON validators.

What is XML?

The XML stands for eXtensible Markup Language. XML was designed to have a simple, formal syntax that makes it easy to transmit and manipulate data over the Internet. The extensible feature of XML is that it does not fix the markup in documents: you can use only the syntax rules of XML to create markup tailored to a specific area. Each XML document can contain multiple objects that refer to one another. A document in XML can include comments, elements, declarations, and instructions for processing. All of these XML elements are structured using XML markup. XML files have the *.xml extension.

XML Example
<?xml version = "1.0" encoding = "UTF-8"?>
<Order>
  <Customer>
     Jack
  </Customer>
  <Rating>
     6.0
  </Rating>
</Order>

What is JSON?

The JavaScript Object Notation (JSON) format is a lightweight and human-readable text format for storing structured data. JSON data files are smaller than the same data in XML, easier for humans to read, and easier to process by computers. JSON is often used for communications between clients and servers on the network. JSON is a format that originated from JavaScript but is now used by almost all programming languages, including PHP, Python, Java, C++, C#, and Go. JSON files have the *.json extension.

JSON Example
{
  "User": "Leo",
  "Quantity": 2,
  "Status":"VIP"
}

How to convert XML to JSON?

To convert XML data strings to JSON follow these simple steps:

  • Copy and paste the XML code into the left input box.
  • Click the "Convert XML to JSON" button.

As a result, you will see the result of XML to JSON conversion in the right pane. You can also convert your JSON code to XML by using our JSON to XML converter.

XML to JSON converter main features

  • Convert XML to JSON in one click.
  • Upload an XML file from your disk.
  • Convert a SOAP envelope or a WSDL document to JSON — the SOAP namespaces stay in the keys.
  • Save your XML in your account for future use.
  • Share source XML and conversion JSON result online.
  • Runs on Windows, macOS and Linux in Chrome, Firefox, Edge and Safari.
  • All conversions run in the browser; the XML you paste is not sent to the server.
  • Beautify the resulting JSON.

XML to JSON converting rules

When performing XML to JSON conversion, the following rules will apply:

  • XML element and attribute names must not contain any delimiter characters.
  • Attributes go into _attributes, element text into _text.
  • Comments arrive as _comment, CDATA as _cdata, the XML declaration as _declaration, DOCTYPE as _doctype, and processing instructions as _instruction.
  • Repeated tags of the same name collapse into a single JSON array. If a tag of another name stood between two of them, it moves after that array, and that is the one case where converting back to XML gives a different element order.
  • Mixed content is not supported: <p>Hello <b>world</b> and bye</p> gives "_text": ["Hello ", " and bye"], and the reverse conversion joins the two text fragments.
  • There is no conversion to JSON primitive types. All XML element and attribute values become JSON strings — <Qty>2</Qty> gives "_text": "2".
XML Input
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Order SYSTEM "order.dtd">
<Order id="7">
  <!-- customer order -->
  <Item>Apple</Item>
  <Note><![CDATA[ raw <b>text</b> ]]></Note>
  <Item>Pear</Item>
  <Qty>2</Qty>
</Order>

JSON Result
{
  "_declaration": { "_attributes": { "version": "1.0", "encoding": "UTF-8" } },
  "_doctype": "Order SYSTEM \"order.dtd\"",
  "Order": {
    "_attributes": { "id": "7" },
    "_comment": " customer order ",
    "Item": [ { "_text": "Apple" }, { "_text": "Pear" } ],
    "Note": { "_cdata": " raw <b>text</b> " },
    "Qty": { "_text": "2" }
  }
}

What are the differences between XML and JSON?

Both JSON and XML are used to organize complex data in a structured and readable form for use in APIs and mobile/desktop applications. Both formats are widely used in client-server communications to transfer data over the Internet. The JSON format is a faster and easier way to exchange data, while the XML format is used with enterprise-grade applications that require increased reliability and security. For more information on the difference between XML and JSON, see the JSON vs XML.

How do I convert XML to JSON in Python, JavaScript, Java or PHP?

The converter on this page runs in the browser. To do the same conversion inside your own code, each language has a standard library for it. The four snippets below produce a dictionary or object from an XML string, which you then serialize to JSON.

Python: xmltodict
import json, xmltodict

with open("order.xml") as f:
    data = xmltodict.parse(f.read())

print(json.dumps(data, indent=2))

JavaScript: fast-xml-parser
const { XMLParser } = require("fast-xml-parser");

const parser = new XMLParser({ ignoreAttributes: false });
const data = parser.parse(xml);

console.log(JSON.stringify(data, null, 2));

Java: Jackson XmlMapper
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xml.getBytes());

String json = new ObjectMapper().writeValueAsString(node);

PHP: simplexml_load_string
$xml  = simplexml_load_string($input, null, LIBXML_NOCDATA);
$json = json_encode($xml, JSON_PRETTY_PRINT);

Each library names the XML-only parts differently: xmltodict prefixes attributes with @, fast-xml-parser uses a configurable prefix, and simplexml_load_string puts them under @attributes. None of them keeps the document order of interleaved tags either.

Benefits of using XML

  • XML tags can be defined by the user
  • XML may contain metadata
  • XML may include schema
  • XML support comments

Benefits of using JSON

  • JSON is compact. The average JSON string is about 2/3 the size of the same data in XML
  • JSON is the default wire format for HTTP APIs
  • JSON serializes and parses in one call in most languages
  • JSON is supported natively in JavaScript

See also