# XML to JSON mapping: attributes, repeated elements and what is dropped

> The exact mapping this converter uses: @ for attributes, #text for text beside attributes, arrays only for repeated names, every value a string, and mixed-content text, comments and declarations discarded.

- Tool: [XML to JSON Converter](https://toolsg.com/en/xml-to-json)
- Language: en (available: en, zh, ms, ta)
- Last verified against official sources: 2026-10-19
- Source site: ToolSG — a planning guide, not official or financial advice. Cite the agencies listed under Sources.

## Key figures at a glance

| Fact | Value | Source |
| --- | --- | --- |
| Attributes | Keys prefixed with @ (for example "@id") — Namespace declarations are attributes too, so xmlns:ns appears as "@xmlns:ns". |  |
| Text beside attributes | Stored under the key "#text" — A leaf element with no attributes becomes a plain string instead. |  |
| Repeated elements | Become an array only when the name occurs more than once — One <item> gives an object or string; two give an array. |  |
| Value types | Always strings — 12.50 stays "12.50"; true stays "true". |  |

## How the conversion works

The page hands your text (trimmed of leading and trailing whitespace) to the browser's `DOMParser` as `application/xml`. If the browser reports a parse error, you see the fixed message "That is not well-formed XML."; the details (line, column, reason) are not shown. Otherwise the root element becomes the single top-level key, and each element is turned into a JSON value by these rules, checked against the real page:

| XML | JSON |
| --- | --- |
| `<a><b>Mei</b></a>` | `{"a":{"b":"Mei"}}` |
| `<a/>` or `<a></a>` | `{"a":""}` |
| `<price currency="SGD">12.50</price>` | `{"price":{"#text":"12.50","@currency":"SGD"}}` |
| `<a x="1"/>` | `{"a":{"#text":"","@x":"1"}}` |
| `<a><b>1</b><b>2</b></a>` | `{"a":{"b":["1","2"]}}` |
| `<a><b>1</b></a>` | `{"a":{"b":"1"}}` |
| `<a><b>1</b><c>x</c><b>2</b></a>` | `{"a":{"b":["1","2"],"c":"x"}}` |
| `<ns:a xmlns:ns="urn:x" ns:id="5"/>` | key `"ns:a"` with `"@xmlns:ns"` and `"@ns:id"` |

Output is indented with two spaces. All values are strings: the converter never guesses numbers or booleans, so `12.50` keeps its trailing zero.

## Worked example

```xml
<people><person id="1" active="true"><name>Mei</name></person><person id="2"><name>Ali</name></person></people>
```

```json
{
  "people": {
    "person": [
      { "@id": "1", "@active": "true", "name": "Mei" },
      { "@id": "2", "name": "Ali" }
    ]
  }
}
```

(The page prints each object over several lines.) With only one `<person>` the same path would be an object, not a one-item array.

## What is lost

- **Mixed content**: in `<a>text<b>bold</b>more</a>` only `{"a":{"b":"bold"}}` survives; the text nodes `text` and `more` are dropped whenever an element has child elements.
- **Text of elements with children and attributes** is likewise dropped; only attributes and children are kept.
- **Comments and processing instructions** are ignored, and the XML declaration is not represented.
- **Order between different names**: siblings are grouped by name, so `b, c, b` becomes `b: [..], c`. Interleaving cannot be recovered.
- **CDATA** is merged into ordinary text: `<![CDATA[1 < 2 & 3]]>` becomes the string `1 < 2 & 3`. Entities are decoded (`&amp;&lt;&#65;` gives `&<A`).
- **Whitespace**: indentation between child elements is ignored, but a text-only element keeps its spaces, so `<a>  </a>` gives `"  "`.

## Errors and strictness

XML is stricter than HTML. The browser rejects: more than one root (`<a/><b/>`), mismatched tags (`<a><b>1</a>`), unquoted attribute values (`<a b=1/>`), repeated attributes on one tag, names starting with a digit (`<1/>`), and entities not defined by the XML spec such as `&nbsp;` (only `amp`, `lt`, `gt`, `apos` and `quot` are predefined). Plain text that is not XML is also rejected. Empty input gives empty output with no error.

Internal DOCTYPE entities are handled by the browser's parser (in a test, `<!DOCTYPE a [<!ENTITY e "hi">]><a>&e;</a>` produced `"hi"`), so the result of DOCTYPE-heavy documents depends on the browser. Do not paste untrusted XML with a DTD and expect uniform behaviour.

> **Tip:** Because one child gives an object and several give an array, code that consumes the JSON must handle both shapes. If you control the XML, consistent wrapper elements make the JSON easier to use; otherwise normalise in code (wrap a non-array in an array).

## Common mistakes

- Treating `"@id"` and `"#text"` as part of your data. They are this tool's conventions; other converters use `_attributes`, `$` or `attr_`.
- Expecting numbers or booleans: every value is text.
- Converting an XML fragment with several top-level elements; wrap it in one root first.
- Assuming round-trip safety. [JSON to XML](https://toolsg.com/en/json-to-xml) does not write attributes, so `@id` becomes an element named `_id`.

## Related tools

Go the other way with [JSON to XML](https://toolsg.com/en/json-to-xml), check or reformat the result with the [JSON formatter](https://toolsg.com/en/json-formatter), browse it in the [JSON viewer](https://toolsg.com/en/json-viewer), or flatten records with [JSON to CSV](https://toolsg.com/en/json-to-csv).

## Frequently asked questions

**Is my data uploaded anywhere?**

No. The conversion runs entirely in your browser using JavaScript. Nothing you paste leaves your device, is logged, or is stored on a server.

**How large a document can it handle?**

The limit is your browser's memory rather than any server quota. Documents of a few megabytes convert without trouble; very large files may make the page briefly unresponsive while it works.

**What happens to nested data?**

Formats that have no concept of nesting store nested values as a JSON string inside the cell, so nothing is silently discarded and the structure can be recovered.

**Does it handle quotes and commas inside fields?**

Yes. Parsing follows RFC 4180, so quoted fields containing commas, escaped double quotes, and line breaks inside a field are all read and written correctly.


## Sources

- [W3C — Extensible Markup Language (XML) 1.0, Fifth Edition](https://www.w3.org/TR/xml/) — Well-formedness rules, root element, attributes, predefined entities, mixed content.
- [IETF — RFC 8259, The JSON Data Interchange Format](https://www.rfc-editor.org/rfc/rfc8259.html) — The JSON output.
- [MDN — DOMParser](https://developer.mozilla.org/en-US/docs/Web/API/DOMParser) — The browser parser the converter relies on.
