XML to JSON
Convert XML to JSON and JSON to XML online, locally in your browser.
Online XML Parser and JSON Converter with Attributes and Namespaces
What Is an XML to JSON Converter?
XML to JSON conversion is the process of parsing XML (Extensible Markup Language) documents and transforming them into JSON (JavaScript Object Notation) data structures. XML represents information using elements, attributes and hierarchical relationships. It is widely used in API responses, configuration files, data exchange and legacy business systems. JSON represents information using objects, arrays and primitive values, making it popular for Web APIs, JavaScript applications and frontend-backend communication.
NZTool XML to JSON converts XML text or local files directly in your browser. It maps element hierarchies, attributes, text content and repeated child elements to JSON. Options include preserving the root element, handling namespace prefixes and empty values, detecting numbers and booleans, and formatting the output. No installation or server upload is required.
How to Convert XML to JSON Online
- Open NZTool XML to JSON and paste XML into the input area or import a local XML file.
- Check that the XML is well formed, including closing tags, quoted attributes and valid nesting.
- Choose whether to preserve the root element, attributes, namespace prefixes and empty elements.
- Enable numeric or boolean detection if needed, and choose JSON indentation for readability.
- Click Convert to parse the XML and display the generated JSON.
- Review the JSON hierarchy, arrays created from repeated elements and attribute mappings.
- Copy or download the JSON, or use the reverse conversion feature for further processing.
Default settings usually work for simple XML. For documents containing attributes, namespaces, repeated elements or mixed content, inspect the result and adjust advanced options when necessary.
Which XML Structures Are Supported?
1. XML Elements and Hierarchies
XML elements are generally mapped to JSON properties, with child elements represented as nested objects. A simple element containing only text may become a JSON string.
<user>
<id>1001</id>
<name>Tom</name>
<city>Tokyo</city>
</user>With the root element preserved, the result is:
{
"user": {
"id": "1001",
"name": "Tom",
"city": "Tokyo"
}
}XML text remains a string by default, so the ID is "1001". When numeric detection is enabled, eligible numeric text may become a JSON number.
2. XML Attributes to JSON
XML attributes and child elements have different meanings. By default, NZTool prefixes attribute names with @ and uses #text for text inside an element that also has attributes.
<product id="101" status="active">Keyboard</product>Converted JSON:
{
"product": {
"@id": "101",
"@status": "active",
"#text": "Keyboard"
}
}This mapping distinguishes attributes from ordinary child elements. Disabling attribute preservation removes attribute information from the output.
3. Repeated XML Elements as JSON Arrays
When a parent contains several child elements with the same name, the converter groups them into a JSON array. This is useful for product lists, orders and collections of records.
<products>
<item>Keyboard</item>
<item>Mouse</item>
<item>Monitor</item>
</products>Converted JSON:
{
"products": {
"item": [
"Keyboard",
"Mouse",
"Monitor"
]
}
}A single child element may produce a scalar value instead of an array. If your API requires a field to always be an array, normalize that structure in the consuming application.
4. Mixed XML Content
Some XML elements contain both text and child elements, as in rich text or document markup. NZTool can use a #content array to represent text segments and nested elements while preserving their main sequence.
<p>Hello <b>World</b>!</p>One possible JSON representation is:
{
"p": {
"#content": [
"Hello ",
{"b": "World"},
"!"
]
}
}Mixed-content conversion helps preserve the sequence of text and markup, but XML and JSON have different data models. The result is not a byte-for-byte backup of the original document.
How Are XML Namespaces Handled?
XML namespaces distinguish elements and attributes from different vocabularies. For example, two systems may both define an item element but use different namespaces.
<root xmlns:a="urn:example:a">
<a:item>100</a:item>
</root>With namespace preservation enabled, the converter can retain prefixed element names. When disabled, prefixes are removed and local names are used.
Removing prefixes may merge distinct elements or obscure their meaning when multiple namespaces share the same local names. Preserve prefixes for complex documents and verify the resulting JSON keys.
How Are Empty XML Elements Converted?
XML may contain empty elements such as <name/> or <name></name>. NZTool lets you map these to an empty string or null.
| XML Input | JSON Output | Explanation |
|---|---|---|
| <name/> | "" | Default empty-string mode |
| <name/> | null | Null mode |
| <count>0</count> | "0" | Text preserved by default |
| <count>0</count> | 0 | Numeric detection enabled |
| <active>true</active> | true | Boolean detection enabled |
Empty strings and null have different meanings in applications. Choose the behavior required by your target API or data format.
Numeric and Boolean Type Detection
Unlike JSON, XML text does not inherently distinguish numeric and boolean values. NZTool preserves ordinary text as strings by default and offers optional type detection.
- Numbers: Convert eligible numeric text to JSON numbers.
- Booleans: Convert recognized values such as true and false to JSON booleans.
- String preservation: Keep identifiers, postal codes, phone numbers and business keys as text.
- Large integers: Keep integers outside JavaScript's safe integer range as strings to avoid precision loss.
For example, 00123 may be an identifier rather than a quantity. Keep it as text. Review type conversion carefully for monetary values, large integers and identifiers that must remain exact.
Does XML to JSON Support CDATA and Comments?
CDATA allows XML documents to contain text that should not be interpreted as markup. The converter reads CDATA content and maps it according to its text-handling rules.
Comment handling is optional. Comments are not normally exported as business data, but supported structures can preserve comment text when the relevant option is enabled.
XML to JSON is a structural conversion, not lossless XML archiving. CDATA boundaries, comment positions, entity spelling, attribute order and some whitespace formatting may not survive a round trip.
XML to JSON vs. JSON to XML
| Feature | XML to JSON | JSON to XML |
|---|---|---|
| Input | XML document | JSON data |
| Output | JSON objects or arrays | XML document |
| Main use | API parsing and data processing | Configuration and data exchange |
| Attributes | Mapped to JSON properties | Mapped from designated JSON properties |
| Repeated elements | Grouped into arrays | Arrays expanded into repeated elements |
| Limitations | XML and JSON types differ | Valid element names and a root are required |
NZTool supports conversion in both directions for everyday data exchange. After reverse conversion, review attributes, arrays, empty values and mixed content.
Common Uses for XML to JSON
- API debugging: Convert XML responses into JSON for JavaScript applications, frontend development and testing.
- Configuration conversion: Organize XML settings into JSON structures.
- Data migration: Convert XML exported from legacy systems into JSON for modern applications.
- Product and order processing: Convert repeated XML elements into JSON arrays.
- Development testing: Inspect hierarchies, attributes, namespaces and data types.
- Learning data formats: Compare XML elements and attributes with JSON objects and arrays.
Security and Format Limitations
NZTool uses the browser's native XML parsing capabilities and performs conversion locally without uploading XML files to a server.
- Each input is limited to 5 MB. Split larger files before processing.
- XML containing DOCTYPE or ENTITY declarations is rejected to reduce entity-related security risks.
- Input must be well-formed XML, with valid tags, attributes and a single root element.
- XSLT transformations, DTD validation and XML Schema validation are not provided.
- Complex namespaces, mixed content and special characters may require additional checks.
- Parsing time and memory usage depend on the document structure and device performance.
For strict XML Schema validation, complete namespace semantics or byte-for-byte round trips, use a specialized XML processing tool.
Frequently Asked Questions
Can I convert an XML file directly to JSON?
Yes. Paste XML or import a local XML file and click Convert.
Do I need to install software?
No. XML parsing and JSON generation run directly in your browser.
Where do XML attributes appear in JSON?
Attributes use the @ prefix by default. For example, id="1001" becomes "@id":"1001". Text in elements containing attributes usually appears under #text.
Why do repeated XML tags become arrays?
Grouping repeated child elements into arrays preserves multiple records instead of overwriting earlier values.
Why are XML numbers quoted in JSON?
XML text is treated as a string by default. Enable numeric detection to convert eligible values. Keep identifiers and large integers as strings.
Can I preserve the XML root element?
Yes. Enable root preservation to include the root element name in JSON, or disable it to output the root's contents directly.
Are XML namespaces supported?
Yes. You can retain or remove namespace prefixes. Retaining them helps avoid ambiguity between similarly named elements.
What happens to empty XML tags?
They can become empty strings or null, depending on your settings and any additional element structure.
Is CDATA supported?
Yes. CDATA text can be read, but its original boundaries are not guaranteed to survive reverse conversion.
Why does XML parsing fail?
Common causes include unclosed tags, unquoted attributes, unescaped characters, multiple root elements and invalid declarations. The tool displays parsing errors and line or column information when the browser provides it.
Are DTD and ENTITY supported?
No. Inputs containing DOCTYPE or ENTITY declarations are rejected to reduce entity expansion and external entity risks.
Can I convert the JSON back to XML?
Yes, using JSON to XML. The resulting XML may differ in comments, whitespace, CDATA boundaries and attribute order.
Is there a file size limit?
Yes. The current limit is 5 MB per input. Split larger XML files.
Is XML uploaded to a server?
No. File reading, parsing, JSON generation and downloading take place locally in your browser.
Free Online XML to JSON Converter
- XML to JSON: Paste XML text or import an XML file to generate JSON.
- Elements and attributes: Preserve hierarchy with @ attributes and #text content.
- Repeated elements: Group matching child elements into JSON arrays.
- Namespaces: Preserve or remove prefixes and optionally retain the root element.
- Data types: Choose empty strings or null and optionally detect numbers and booleans.
- Complex content: Handle CDATA, mixed content and optional comments.
- Validation and export: View errors, format JSON, use examples, copy, download or reverse convert.
- Privacy: Local browser processing without uploads, with a 5 MB input limit.