Converting Between JSON and XML: Structure Mapping, Lost Data, and When to Use Each
JSON and XML are both common formats for structured data, but their design philosophies differ, so conversion between them has quirks. This article explains how objects, arrays, and attributes map to each other, the information easily lost in conversion, when to use each format, and the pitfalls of handling attributes, namespaces, and types.
Web APIs tend to use JSON, while configuration files and enterprise system integrations often use XML. The need to "convert this JSON to XML" or "process received XML as JSON" comes up all the time. The two look similar, but because their design philosophies differ, a mechanical conversion can quietly drop information you did not expect to lose. This article walks through converting between JSON and XML step by step, from how the structures map to each other, to the information easily lost, to the pitfalls of conversion.
The Basic Difference Between JSON and XML
JSON (JavaScript Object Notation) is a lightweight data format centered on key-value pairs. Its hallmark is that it expresses structures close to programming-language data types directly: objects ({}), arrays ([]), strings, numbers, booleans, and null. It is compact and can be parsed as-is in JavaScript and many other languages, so it is widely used for Web API responses.
XML (Extensible Markup Language), by contrast, is a markup format similar to HTML that wraps elements in opening and closing tags. Beyond nesting elements, it lets you add attributes to tags, avoid tag collisions with namespaces, and define structure strictly with comments and schemas (DTD, XML Schema). It is rich as a document format, has a long history, and remains firmly used for business-to-business data exchange, configuration files, and document markup.
In short, JSON is good at "representing data structures plainly," while XML is good at "attaching rich metadata as a document." This very difference in design philosophy is what makes conversion less than straightforward.
Structure Mapping: Objects, Arrays, and Attributes
The first step to understanding conversion is grasping how the building blocks of each format correspond. The typical mappings are as follows.
| JSON | XML | Notes |
|---|---|---|
Object {} | An element with child elements | The key name becomes the element name |
| Key-value pair | Element name and text content | Or expressed as an attribute |
Array [] | Repeated elements of the same name | There is no dedicated array notation |
| String / number / boolean | Text node | In XML everything is essentially a string |
For example, JSON {"book": {"title": "Intro to XML"}} maps to XML like <book><title>Intro to XML</title></book>, where the key becomes the element name and the value becomes text. That is a relatively plain mapping.
The tricky part is arrays. XML has no notation that directly represents an array, so it usually expresses one by repeating elements of the same name. {"item": ["A", "B"]} becomes <item>A</item><item>B</item>, but in the reverse direction you cannot mechanically decide whether "a single element of that name" is one element or an array of length one. This is where the rules diverge from one converter to another.
XML-specific attributes are another sticking point. When moving an attribute like <book id="1"> to JSON, many converters represent it with a prefixed key such as {"book": {"@id": "1"}}. The element's text content is commonly assigned to a special key like #text. These conventions for "how to express concepts JSON lacks" are not standardized and differ by tool.
Information Easily Lost in Conversion
Because JSON and XML do not fully overlap in what they can express, conversion is not necessarily reversible (a round trip may not return the original). The following information is especially prone to being lost or altered, so take care.
- The attribute-versus-element distinction: after XML to JSON drops attributes into
@-prefixed keys, converting JSON back to XML can turn those attributes into elements if the conventions do not match. - Element order: in XML the order of child elements can be meaningful, but the keys of a JSON object are not guaranteed to preserve order. In order-sensitive documents, information is lost.
- Comments, namespaces, and processing instructions: XML comments, namespace declarations, and processing instructions like
<?xml ?>have no JSON counterpart and tend to be discarded during conversion. - Data types: JSON numbers and booleans become plain strings in XML. Conversely, whether to treat XML's
"123"as a number requires judgment, so type information becomes ambiguous. - Empty elements and null: whether to treat an empty XML element (
<a/>) as an empty string or null varies by tool.
These losses stem from the fact that conversion is "transcribing a data structure," not "a fully equivalent transformation." Always check the content after converting, and inspect it visually with formatting if needed. To tidy up XML structure so it is easy to read, the XML Formatter is helpful.
When to Use Each Format
Which format you should use depends on the purpose in the first place. Before converting, it is also important to judge whether conversion is truly necessary.
Where JSON fits
JSON suits lightweight data that programs read and write: Web APIs, communication with single-page applications, parts of configuration files, and logs. It is compact, can be handled as-is in JavaScript and many languages, and parses quickly. It is the first choice when you want to represent a data structure plainly.
Where XML fits
XML suits business-to-business data exchange (EDI), financial and government formats, document-centric data (such as the internal format of office documents), and cases where you want to validate structure strictly with a schema. Mechanisms like attributes, namespaces, and schemas let you define complex, long-lived data rigorously. When an existing system assumes XML, handling XML directly can be safer than forcing everything into JSON.
Conversion becomes necessary in situations that bridge different worlds, such as "we want to handle an external API that serves XML inside our JSON-based system." In that case, the JSON XML Converter lets you try conversions quickly in the browser.
Conversion Pitfalls: Attributes, Namespaces, and Types
When you actually convert, keep these three common stumbling blocks in mind.
1. Decide how attributes are handled. For XML to JSON, understand the tool's convention, such as whether attributes become @-prefixed keys or are grouped into a separate object. If you want a clean round trip back, it is safest to convert both ways with a converter that uses the same convention.
2. Watch out for namespaces. A namespaced tag like ns:tag may become a key name containing a colon in JSON, or the prefix may be dropped. For data where namespaces carry meaning, check that no collisions or omissions occurred after conversion.
3. Be mindful of type conversion. Since all XML values are strings, "true" or "123" can stay as strings after converting to JSON. If the receiving side needs to treat them as numbers or booleans, you need to adjust (cast) the types after conversion. Conversely, JSON to XML drops type information, so defining types in the schema is safer.
All of these arise from "the difference in expressive power between JSON and XML." Conversion is convenient, but understand that no universal automatic conversion exists; deciding on conventions and a verification procedure that match your data's structure is the shortcut to avoiding trouble.
Trying an Actual Conversion
Once you understand the theory, the fastest path is to try it with real data. Paste your JSON or XML into the JSON XML Converter and convert both ways, then see with your own eyes how arrays expand into same-named elements and what keys attributes become. Round-tripping a small sample makes it clear where information changes.
When the converted XML is tangled and hard to read, tidying the indentation with the XML Formatter lets you grasp the structure at a glance. Element nesting and attribute positions become easier to see, which makes it much easier to check that conversion did not produce an unintended shape. Converting between JSON and XML comes down to understanding the mappings and the information that is easily lost, then trying small and verifying visually. Master these basics and you can smoothly bridge worlds that use different formats.
Please note that this article is intended to provide general information. Actual conversion specifications and tool behavior may differ depending on the library and convention. When handling important data, make your decision after checking the latest official specifications and documentation.
FAQ
Should I use JSON or XML?
It depends on the purpose. JSON suits Web APIs and lightweight data exchange between programs; it is concise and parses quickly. XML suits business-to-business data exchange and document-centric data where you want to define structure strictly with attributes, namespaces, and schemas. It is not about superiority; choose based on the nature of your data and the assumptions of your existing systems.
How is a JSON array represented in XML?
XML has no dedicated array notation, so it is usually represented by repeating elements of the same name. For example, {"item":["A","B"]} becomes <item>A</item><item>B</item>. In the reverse direction, when there is only one element of a given name, you cannot mechanically tell whether it is a single element or an array of length one, so rules differ by tool.
What happens to XML attributes when converting to JSON?
Since JSON has no attribute concept, many converters represent them as prefixed keys such as {"book":{"@id":"1"}}. The element text content is commonly assigned to a special key like #text. These conventions are not standardized and differ by tool, so if you round-trip, it is safest to use the same convention both ways.
Is any information lost during conversion?
Yes. XML comments, namespaces, processing instructions, and child-element order have no JSON counterpart and tend to be lost or altered during conversion. JSON numbers and booleans also become strings in XML, making type information ambiguous. Conversion is not a fully equivalent transformation, so verifying the result afterward is essential.
Is the data I paste into the JSON XML Converter sent to a server?
WAZA TOOLS' JSON XML Converter and XML Formatter run entirely in your browser, so the data you paste is never sent to or stored on a server. They work on smartphones too, and you can convert and format as many times as you like, for free.