XML & JSON Conversion Guide: Config Files and API Data Format Transformation
Core Differences: XML vs JSON
| Feature | XML | JSON |
|---|---|---|
| Data format | Tag-based markup | Key-value pairs |
| Attributes | Yes (<node attr="val">) |
No (attributes become fields) |
| Namespaces | Yes (xmlns) |
No |
| Comments | Yes (<!-- -->) |
No |
| Arrays | Implicit (repeated elements) | Explicit ([]) |
| Text nodes | Mixed content (tags + text) | Plain string values |
XML is better for document-style data (SVG, HTML, configs). JSON is better for structured data (API responses).
How XML Attributes Map to JSON
XML elements can have both attributes and text content—this is the tricky part of conversion:
Simple Element
<person name="John" age="30"/>
{
"person": {
"@name": "John",
"@age": "30"
}
}
Convention: use
@prefix for XML attributes. This is the most common conversion rule.
Mixed Content (Attributes + Text)
<price currency="USD">68.00</price>
{
"price": {
"@currency": "USD",
"#text": "68.00"
}
}
Use
#textfor the element's text content, distinguishing it from attributes.
Array Detection: The Most Common Pitfall
In XML, arrays are implicit—repeated elements with the same name form an array:
<books>
<book>Three-Body Problem</book>
<book>To Live</book>
</books>
{
"books": {
"book": ["Three-Body Problem", "To Live"]
}
}
Problem: With a single element, XML can't distinguish it from a non-array:
<books>
<book>Three-Body Problem</book>
</books>
This might convert to:
{
"books": {
"book": "Three-Body Problem"
}
}
Solution: In the XML↔JSON tool, check "Force array" and specify which elements should always be treated as arrays.
Namespace Handling
XML namespaces have no direct equivalent in JSON:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<m:GetPrice xmlns:m="http://example.com">
<m:Item>123</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
Common strategies:
| Strategy | Description | Use Case |
|---|---|---|
| Keep prefix | soap:Body → "soap:Body" |
SOAP/WSDL processing |
| Strip prefix | soap:Body → "Body" |
Simple cases, no conflicts |
| Namespace key | {soap}Body |
Need to distinguish same-name elements from different namespaces |
Use the XML Formatter to tidy up XML before conversion.
Using the XML↔JSON Tool
Step 1: Open the Tool
Visit the XML↔JSON tool and select the conversion direction.
Step 2: Enter Source Data
- XML → JSON: Paste XML string
- JSON → XML: Paste JSON string
You can prettify input first with JSON Formatter or XML Formatter.
Step 3: Configure Conversion Options
| Option | Description |
|---|---|
| Attribute prefix | Map attributes as @attr or $attr |
| Text node key | Map text content as #text or _text |
| Force array | Specify element names that should always be arrays |
| Namespaces | Keep / strip / transform |
Step 4: Execute Conversion
Click "Convert", review the result, and copy.
Practical Scenarios
Config File Migration
Many legacy projects use XML configs (Spring, Maven). New projects prefer YAML/JSON:
pom.xml → package.json (dependency info)
spring-config.xml → application.json (Bean config)
SOAP API to REST
SOAP responses are XML; they need conversion to JSON for front-end use:
SOAP XML response → JSON → front-end rendering
SVG Metadata Extraction
SVG is XML format; extract metadata to JSON for programmatic processing:
<svg width="100" height="100"> → {"svg": {"@width": "100", "@height": "100"}}
JSON to XML Considerations
When converting JSON to XML, note:
| Consideration | Description |
|---|---|
| Root element | JSON must have a single root key |
| Key name validity | XML tag names can't start with a digit or contain spaces |
| Array wrapping | JSON arrays need a wrapper element name |
| Special characters | <, >, & must be escaped as entities |
Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Single element not converted to array | XML can't distinguish single element from array | Check "Force array" |
| Attributes lost | Conversion rules missing attribute prefix | Check attribute prefix setting |
| Namespace conflict | Same-name elements in different namespaces | Keep namespace prefixes |
| JSON-to-XML error | Invalid key name | Rename key to comply with XML spec |
| Mixed content lost | Text node key not correctly mapped | Check #text configuration |
Summary
XML↔JSON conversion is a common need in data format migration and API integration. Understanding the three core issues—attribute mapping, array detection, and namespaces—is key to correct conversion. The XML↔JSON tool provides flexible conversion options, while JSON Formatter and XML Formatter complete your format conversion workflow.