Converting JSON to XML involves transforming the data from the JSON format (which is key-value pairs) into XML tags. This is useful when you need to convert data into a machine-readable format for XML-based applications or systems.
Example JSON:
json
[
{
"Name": "John",
"Age": 28,
"Country": "USA"
},
{
"Name": "Jane",
"Age": 22,
"Country": "Canada"
},
{
"Name": "Tom",
"Age": 30,
"Country": "UK"
}
]
Converted XML Output:
xml
<data>
<row>
<Name>John</Name>
<Age>28</Age>
<Country>USA</Country>
</row>
<row>
<Name>Jane</Name>
<Age>22</Age>
<Country>Canada</Country>
</row>
<row>
<Name>Tom</Name>
<Age>30</Age>
<Country>UK</Country>
</row>
</data>
How to Convert JSON to XML
1. Using JavaScript (For Web Use)
You can use JavaScript to convert JSON to XML directly in a browser. Here's an example:
JavaScript Code:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to XML</title>
</head>
<body>
<h2>Convert JSON to XML</h2>
<button onclick="generateXML()">Generate XML</button>
<pre id="xml-output"></pre>
<script>
const jsonData = [
{
"Name": "John",
"Age": 28,
"Country": "USA"
},
{
"Name": "Jane",
"Age": 22,
"Country": "Canada"
},
{
"Name": "Tom",
"Age": 30,
"Country": "UK"
}
];
function generateXML() {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n<data>\n';
// Loop through the JSON array and convert it to XML
jsonData.forEach(item => {
xml += ' <row>\n';
for (let key in item) {
xml += ` <${key}>${item[key]}</${key}>\n`;
}
xml += ' </row>\n';
});
xml += '</data>';
// Display the XML output in the <pre> tag
document.getElementById("xml-output").textContent = xml;
}
</script>
</body>
</html>
Explanation:
JSON Data: The jsonData variable holds the JSON data.
generateXML() Function: This function:
Converts the JSON array into XML format.
Loops through each JSON object and creates XML tags dynamically.
The XML output is displayed in the <pre> tag to preserve formatting.
Steps:
Copy and paste the code into an HTML file.
Open the file in a web browser.
Click the "Generate XML" button to see the XML output.
2. Using Python (For Server-Side or Automated Conversion)
For bulk conversion or automation, you can use Python to convert JSON to XML. Here's how you can do it:
Python Script:
python
import json
import dicttoxml
# Sample JSON data
json_data = [
{"Name": "John", "Age": 28, "Country": "USA"},
{"Name": "Jane", "Age": 22, "Country": "Canada"},
{"Name": "Tom", "Age": 30, "Country": "UK"}
]
# Convert JSON to dictionary format (this is optional if JSON is already a dictionary)
data_dict = json.loads(json.dumps(json_data))
# Convert dictionary to XML
xml_data = dicttoxml.dicttoxml(data_dict, root=True)
# Save to an XML file
with open('output.xml', 'wb') as f:
f.write(xml_data)
print("XML file 'output.xml' has been created.")
Explanation:
json.loads() and json.dumps(): Convert the JSON data into a Python dictionary (if needed).
dicttoxml Library: This is a Python library that can easily convert Python dictionaries to XML format.
The resulting XML is saved to an output.xml file.
Steps:
Install the dicttoxml library: pip install dicttoxml.
Save the script as a .py file.
Run the script, and the XML file will be saved to output.xml.
3. Using Online Tools
You can use online tools to quickly convert JSON to XML without writing code:
JSON to XML Converter:
Paste your JSON data into the tool.
The tool will convert the JSON into XML format, which you can copy or download.
ConvertJSON:
Paste your JSON data or upload a JSON file.
The tool will generate an XML output.
4. Manually Writing XML from JSON
For small datasets, you can manually convert JSON to XML by following this structure:
JSON:
json
[
{"Name": "John", "Age": 28, "Country": "USA"},
{"Name": "Jane", "Age": 22, "Country": "Canada"}
]
Manually Written XML:
xml
<data>
<row>
<Name>John</Name>
<Age>28</Age>
<Country>USA</Country>
</row>
<row>
<Name>Jane</Name>
<Age>22</Age>
<Country>Canada</Country>
</row>
</data>
Summary of Methods:
JavaScript: Use JavaScript to convert JSON to XML in the browser. Ideal for on-the-fly conversions in web applications.
Python: Use Python with the dicttoxml library for server-side or bulk conversion, ideal for automation or large datasets.
Online Tools: Use JSON to XML Converter or ConvertJSON for quick, no-code conversion.
Manual Writing: For small datasets, you can manually convert JSON to XML by following the structure of the data.