Online XML/JSON Converter: Convert Between Data Formats
What Are XML and JSON
XML (eXtensible Markup Language) is a markup language for marking data, supporting attributes and namespaces.
JSON (JavaScript Object Notation) is a lightweight data exchange format using key-value pairs.
Comparison Example
XML:
<user id="1">
<name>Alice</name>
<email>alice@example.com</email>
<bio><![CDATA[score: a < b]]></bio>
<tags>
<tag>developer</tag>
<tag>admin</tag>
</tags>
</user>
JSON (this site's defaults):
{
"user": {
"@id": "1",
"name": "Alice",
"email": "alice@example.com",
"bio": {
"#cdata": "score: a < b"
},
"tags": {
"tag": ["developer", "admin"]
}
}
}
Attributes land on @id, CDATA under #cdata, and repeated siblings become arrays — the ToolVault converter defaults. You can turn attributes off or force every element into an array on the tool page.
XML vs JSON Comparison
| Feature | XML | JSON |
|---|---|---|
| Readability | Medium | High |
| File size | Large (tag redundancy) | Small |
| Data types | ❌ All strings | ✅ Multiple types |
| Attribute support | ✅ Native | ⚠️ Convention-based |
| Namespace | ✅ Supported | ❌ Not supported |
| Comments | ✅ Supported | ❌ Not supported |
| Schema validation | ✅ XSD | ✅ JSON Schema |
| Web standard | Legacy | Modern |
Why You Need XML/JSON Conversion
- Web service migration: SOAP (XML) → REST (JSON) API upgrade
- Config file conversion: Legacy XML config → modern JSON config
- Data integration: Different systems use different formats
- RSS/Atom parsing: RSS Feed (XML) → JSON processing
- Office documents: .docx/.xlsx are XML-based, need JSON conversion
- Legacy system integration: New systems connecting to XML-based old systems
Common Conversion Challenges
| Challenge | Description | What this tool does |
|---|---|---|
| Attributes vs elements | No standard JSON form for attributes | Default @attr prefix; optional off |
| Repeated elements | Sometimes one, sometimes many | Sibling repeats → arrays; force-array tags or wrap-all |
| Text / CDATA | Mixed content and raw fragments | #text / #cdata; optional keep whitespace |
| Namespaces | Prefixes enter key names | Keeps ns:tag literally — agree upstream |
| Malformed XML | Mismatched tags silently accepted | Well-formed validation with line:col |
How to Use an Online Tool
Using ToolVault's XML/JSON Converter:
- Paste XML and click “XML → JSON” (or the reverse with JSON)
- Toggle options: keep attributes, keep CDATA, wrap all elements as arrays, keep text whitespace
- For a stable array shape on only some tags, list local names under “Force-array tags” (e.g.
role,item) - On failure, read the line:col badge and context pointer instead of re-reading the whole blob
- Everything runs in the browser; nothing is uploaded
Note: conversion is button-triggered, not silent auto-detect, and there is no server-side streaming pipeline for huge files — sample and agree conventions first.
Conversion in Code
JavaScript (close to this site's defaults)
const { XMLBuilder, XMLParser } = require('fast-xml-parser');
// XML → JSON (validate — otherwise bad tags may pass silently)
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@',
cdataPropName: '#cdata',
textNodeName: '#text',
processEntities: false,
});
const xml = '<user id="1"><name>Alice</name></user>';
const json = parser.parse(xml, true);
// JSON → XML
const builder = new XMLBuilder({
ignoreAttributes: false,
attributeNamePrefix: '@',
cdataPropName: '#cdata',
format: true,
});
const xmlStr = builder.build(json);
Python
import xmltodict
import json
# XML → JSON
xml_str = '<user id="1"><name>Alice</name></user>'
data = xmltodict.parse(xml_str)
json_str = json.dumps(data, indent=2)
# JSON → XML
data = {"user": {"name": "Alice"}}
xml_str = xmltodict.unparse(data, pretty=True)
Attributes, namespaces, and mixed content: the unsolvable triangle
Three XML features have no natural JSON home, and every converter picks different answers. Attributes: prefix conventions (@id) or child objects — pick one and stay consistent. Namespaces: ns:element flattens to keys or drops the URI (collisions follow); SOAP and enterprise schemas live here. Mixed content is hardest — JSON invents #text; enable “Keep text whitespace” when spacing matters. Document-heavy mixed XML usually should stay XML.
Array shape: why force-fold exists
Downstream TypeScript or JSON Schema often assumes tag: string[], but a single occurrence usually becomes a scalar tag: "admin" — and runtime breaks. Fix it three ways: guarantee repeats at the source, accept both shapes in consumers, or stabilize at convert time with force-array tags / wrap-all-arrays. Put that on the integration checklist.
DTDs, XXE, and why converters disable entities
XML parsers that resolve Document Type Definitions can be coerced into XXE: a crafted DTD reads local files or SSRFs internal endpoints. The converter keeps entity processing and external entities off; verify any tool you feed untrusted XML into does the same. A converter that happily resolves entities is a file-disclosure primitive aimed at its own environment.
Schema round-trip: when JSON schemas cannot express XML
XML Schema (XSD) expresses ordering (sequence), choice groups, and mixed content constraints that JSON Schema cannot — an XSD-to-JSON-Schema conversion is always lossy. Order-sensitive data degrades silently when objects lose element order. Before converting: does anything downstream depend on order, namespaces, or attributes? If yes, keep XML at the boundary.
A conversion checklist before you ship the mapping
- Decide attribute policy (
@attrvs nested object) and document it for both directions - List tags that must always be arrays — enable force-array / wrap-all on the XML/JSON converter
- Confirm whether whitespace and CDATA must survive (
#text/#cdata) - Reject untrusted XML that needs DTD/entity expansion — keep entities off
- Round-trip a golden fixture in CI; fail the build if key shape drifts
Skipping (2) is the most common production break: a one-item XML list becomes a JSON scalar on Tuesday and an array on Wednesday when a second item appears.
FAQ
How to Convert XML Attributes to JSON?
No single standard exists. This site defaults to @ for attributes (e.g. @id), #text for text, and #cdata for CDATA. Turn off “Keep attributes” on the tool page to drop them.
Does JSON to XML Lose Information?
Possibly. Arrays need element-name conventions; JSON types often become text in XML; without an attribute prefix, attributes and children misalign. Align naming and array shape before converting.
Why Did Bad Tags “Convert” Before?
Some parsers skip pair validation and rewrite mismatches quietly. This tool validates well-formedness and reports line:col so you can fix the document.
When to Use XML vs JSON?
Use XML for namespaces, strict Schema validation, and mixed document content (e.g. XHTML). Use JSON for Web APIs, config files, and frontend–backend exchange. Modern projects lean JSON, but plenty of boundaries still speak XML.
Triage notes
'The unsolvable triangle' admits that attributes, namespaces, and mixed content have no natural home in JSON and that every converter answers differently — so the rule given is not 'pick the right answer' but 'pick one answer and keep the whole pipeline consistent'. The array-shape section grabs a runtime bomb: an element that appears only once in XML becomes a scalar, and downstream code assuming tag: string[] detonates at runtime; pick one of the three remedies and write it into the integration checklist. The XXE section's line of persuasion is heavy — a converter happy to resolve entities is a file-disclosure primitive pointed at its own environment — and the well-formedness check corrects 'it converted before': some parsers skip pairing validation by default and silently rewrite mismatched closing tags, where this site chooses to error out with a line number.
This article is brought to you by ToolVault. More developer tools at the homepage.
Related Tools
Related Articles
Online CSV to SQL Tool: Import Tabular Data into Databases
CSV to INSERT with empty cells → NULL, MySQL backticks or PostgreSQL quotes, BOM stripping, Markdown pipe escaping, and when to switch to COPY/LOAD instead.
HTML Table to JSON: How to Preserve Rows, Columns and Structure
Converting an HTML table from a web page to JSON — how to keep headers, handle merged cells and nested structure? Step-by-step with our HTML table to JSON tool.
Excel to JSON: Dates Become Numbers / Chinese Garbled — Fix
When converting Excel to JSON, dates turn into numbers like 44927, Chinese is garbled, empty cells vanish? Explains Excel serial dates and encoding pitfalls, with our Excel to JSON tool.