Compare formats and convert between them for APIs and data exchange
JSON and XML are both plain-text ways to describe structured data, and plenty of developers have to handle both in the same week: a REST API returns JSON, a payment gateway or government portal still wants XML, and an old config file sits somewhere in between. This guide explains how the two formats differ, when each one is the better choice, and what actually happens when you convert between them, including the parts that do not translate cleanly.
Here is one small record written both ways. In JSON:
{"book": {"@id": "7", "title": "Clean Data", "tag": ["json", "xml"], "price": 12.5, "inStock": true}}
And in XML:
<book id="7">
<title>Clean Data</title>
<tag>json</tag>
<tag>xml</tag>
<price>12.5</price>
<inStock>true</inStock>
</book>
The XML version is longer because every value needs an opening and a closing tag. It also has something JSON does not: the id is an attribute on the element rather than a child. The @id key in the JSON is a common convention for representing that attribute, which we come back to below.
JSON has six kinds of value: objects, arrays, strings, numbers, booleans (true/false) and null. That small set maps straight onto the data structures of almost every programming language, which is why nearly all modern web APIs, mobile apps and NoSQL databases use it. It is compact, quick to parse, and easy to read once it is formatted. Its weak spots are that it has no comments, no built-in way to say "this field is a date", and validation only comes from an add-on standard (JSON Schema) that many projects never use.
XML is a markup language: everything is an element with a name, optional attributes, text and child elements. It supports namespaces (so two vocabularies can be mixed without name clashes), comments, CDATA blocks for raw text, and strict validation through XSD schemas. That is why it is still the format behind SOAP web services, many banking and e-invoicing standards, RSS and Atom feeds, SVG images, Android layouts and the insides of .docx and .xlsx files. The cost is verbosity and more complex parsing.
The two formats do not map one-to-one, so every converter has to follow a convention. Our JSON to XML and XML to JSON tools use the widely used xmltodict-style rules, and it helps to know them before you trust the output:
@, so <item id="5"> gives "@id": "5". Going the other way, a JSON key that starts with @ is written as an attribute.#text: <note type="a">hello</note> becomes {"note": {"@type": "a", "#text": "hello"}}.<tag> siblings become "tag": ["json", "xml"], but a single <tag> becomes a plain string. If a feed sometimes has one item and sometimes many, normalise it in your code before looping.12.5 and true come out as the strings "12.5" and "true"; convert them yourself where it matters. In the other direction, JSON null becomes an empty element such as <nick />."first name" becomes <first_name> and "2fa" becomes <_2fa>.<ns:price> becomes "price". That makes the JSON easier to use but means the namespace is not recorded.<p>Hello <b>world</b>!</p>, the text pieces are joined into #text and the child goes under its own key, so the exact position of text between tags is not kept. For HTML-like documents, use a proper XML or HTML library instead.Because of these rules, simple documents survive a round trip (XML to JSON and back) with the same structure, but types, namespaces and the order of mixed text do not. Keep the original file as your source of truth.
A converter can only work with well-formed input. Common problems are a trailing comma or single quotes in JSON, and an unclosed tag, an unescaped & or two root elements in XML. Run JSON through the JSON formatter first; it points out syntax errors and pretty-prints valid input. If your JSON is badly broken, the fix invalid JSON guide walks through the usual causes. Our XML to JSON tool reports the line and column of the first XML error, and the XML formatter makes a long document readable before you start.
Valid for the converter is not the same as valid for the system you are sending to. If the receiving service publishes an XSD schema, check the converted XML against it or do a test submission, because schemas often require a fixed element order, specific namespaces or formats such as ISO dates.
The two converters on this site send your text over HTTPS to our server, convert it for that request and do not save it. That is fine for sample payloads and public data. For production data with customer details, API keys or passwords, use a library in your own environment instead (xmltodict in Python, fast-xml-parser in JavaScript or Jackson in Java), which is also the better choice for large or automated jobs.
Neither is better in general. JSON is simpler and the default for web APIs; XML is stronger for documents, namespaces and schema-validated exchange. Use what the system on the other side expects.
Not natively. JSON only has keys and values, so converters mark attributes with a prefix, most often @, which is what our tools use.
XML has no data types without a schema, so every value is text. After converting XML to JSON, parse the fields you need as numbers, booleans or dates in your code.
Wrap your data in one top-level key, such as {"invoice": {...}}; that key becomes the root element. Otherwise the converter uses <root>.