XML to JSON Converter
Paste XML to get JSON, or JSON to get XML. XML has attributes, text beside child elements and repeated elements; JSON has none of these, so every converter has to pick conventions. Here you pick them, and a live sample shows exactly how your settings read a small element. Errors name the line and column, the tool never fetches a DOCTYPE or expands an entity, and everything runs in your browser — nothing is uploaded. Free, no sign-up.
Converted in your browser as you type. Nothing is uploaded, and nothing is fetched, including anything a DOCTYPE points to.
Conventions
How these settings read an element
<book id="7"><title>Dune</title><tag>sf</tag><tag>classic</tag><note lang="en">Classic</note><sale/></book>
{
"book": {
"@id": "7",
"title": "Dune",
"tag": [
"sf",
"classic"
],
"note": {
"@lang": "en",
"#text": "Classic"
},
"sale": null
}
}Paste some XML or JSON, or load the example, to convert it.
How to Use the XML to JSON Converter
- 1. Choose a direction. XML → JSON parses the XML and writes JSON; JSON → XML writes elements from the JSON. Load the example fills in a small product catalogue for either direction.
- 2. Set the conventions. Pick how attributes are named and which key holds text. For XML input, also choose when children become arrays, whether numbers and booleans are typed, what an empty element becomes, and whether the root element's name is kept. The panel at the bottom of the options applies your choices to a fixed sample, so you can see the mapping before you convert.
- 3. Paste your text. The result updates as you type. A problem is reported with its line and column, a plain-English reason, and the offending line with a caret.
- 4. Read the notes, copy or download. Warnings cover mixed content and name clashes. Use the result as input and swap direction checks a round trip in one click.
Why There Is More Than One Right Answer
XML elements can carry attributes, hold text, hold child elements, hold all three, and repeat. JSON has objects, arrays and values. Mapping one onto the other forces choices, and different tools make different ones: attributes get an @ or a _ prefix or a nested object, text lands under #text, $ or _, and an element is an array only when it repeats, or always. None is wrong; the problem is when one side assumes a convention the other did not use. That is why this tool shows its conventions instead of hiding them.
| Setting | Choices | What it changes |
|---|---|---|
| Attribute names | @name, @_name, _name, -name | The prefix that marks a JSON key as an XML attribute, in both directions. |
| Text key | #text, _text, $, text | Where text goes when an element also has attributes or children. |
| Child elements | Arrays only for repeats, or always arrays | A single child is a value or a one-item list. Always-arrays gives every consumer the same shape. |
| Numbers and booleans | Off or on | Off keeps every value as a string. On reads true, false and JSON-style numbers as such, and writes numbers exactly as they appeared, so 1.50 stays 1.50. |
| Empty elements | null or an empty string | What <a/> and <a></a> become. |
| Root element | Keep its name, or drop it | {"catalog": {…}} or just the contents. |
A Worked Example
The XML example has eleven elements and five attributes, three levels deep. With the default settings:
- The two
productelements repeat, soproductis an array; the singlenoteis a plain value. pricehas an attribute and text, so it becomes an object:"@currency": "USD"and"#text": "1.50". The text stays a string, which keeps the trailing zero.- The two
tagelements inside the first product become an array; the second product has none. <discontinued/>becomesnull, andInk & Cartridgeis decoded toInk & Cartridge.- The comment
two productsis dropped: JSON has nowhere to put it.
From JSON to XML
Going back, the same conventions run in reverse. A key with the attribute prefix becomes an attribute; the text key becomes text; an array becomes the same element repeated; null becomes an empty element. If the JSON has exactly one top-level key, that key is the root element, otherwise the root name you set is used (and a bare list becomes repeated item elements). Text and attribute values are escaped, and names are checked: a key such as first name, or one that starts with a digit, is not a legal XML name, so the tool reports it instead of writing broken XML. A list inside a list has no XML form and is reported too.
What It Checks, and What It Does Not
The parser checks well-formedness: every tag closed and nested properly, one root element, quoted attribute values, no duplicate attributes, valid references. It does not validate against a DTD or XML Schema. A DOCTYPE is skipped with a warning, an entity it declares is not expanded, and nothing is ever fetched, so the usual entity-expansion and external-entity attacks do nothing here. The five predefined entities and numeric character references are decoded.
What does not survive
Comments, processing instructions and the XML declaration are dropped on the way to JSON. CDATA sections become ordinary text. Namespace prefixes stay in names (a:b) and xmlns declarations stay as attributes, but namespaces are not resolved. When an element mixes text and child elements, the text is kept but its position among the children is not, and the tool warns.
Frequently Asked Questions
- How do I convert XML to JSON?
- Paste the XML into the box with the direction set to XML → JSON. The JSON appears as you type. Check the sample at the bottom of the options to see how attributes, text and repeated elements are mapped, adjust the conventions if the consumer of the JSON expects different ones, then copy the result or download it.
- What happens to XML attributes?
- Each attribute becomes a key on the element's object, written with the prefix you choose, such as
@id. An element that has attributes becomes an object even if it also has text, in which case the text is stored under the text key. JSON → XML reads the prefix back and writes the attribute. - Why is one element an object but two elements an array?
- With the default setting, a child that appears once is a plain value and a child that appears more than once is an array, which is what most XML-to-JSON tools do. It means the shape depends on the data. Choose Always arrays and every child becomes a list, so code that reads the JSON never has to check which one it got.
- What happens to comments, CDATA and the XML declaration?
- Comments, processing instructions and the declaration are dropped because JSON has no place for them. A CDATA section is read as ordinary text, so its angle brackets come back escaped if you convert to XML again.
- Can it handle mixed content, such as a paragraph with bold words?
- It keeps the text and the child elements but not the order between them, and it warns. For documents like that, which are prose with markup, XML is the right format and a JSON copy loses information. For HTML pages, use the HTML to Markdown Converter instead.
- Does it validate against a schema, and is a DOCTYPE safe?
- It checks that the XML is well-formed, not that it follows a DTD or XML Schema. A DOCTYPE is skipped with a warning; entities it declares are not expanded and nothing it points to is fetched, so an entity bomb or an external-entity request cannot do anything.
- Is my data uploaded?
- No. The parsing and writing are plain JavaScript in your browser, so nothing you paste is sent to CitationEasy or anywhere else.