How to Convert XML to JSON Files? Free & Reliable Ways
This guide will explain ways to convert XML to JSON format using Copy-Paste, Coding language, and best automatic offline solution for nested and complex data files. This write-up will answer all related question like does direct option available, how to do it manually and setbacks, and what is the alternate solution for nested and large XML files.
XML format uses nested tags and attributes to store large dataset in a hierarchical structure. On the other hand, JSON is a lightweight format as well as human-friendly. Multiple applications and database use this format to process data.
This article will guide you through various methods and tools on how to parse XML files to JSON, from manual approaches to leveraging powerful programming libraries and dedicated software.
How to Convert XML to JSON Format?
Transforming XML to JSON format is not an easy task, but we have suggested possible and easiest techniques below one by one:
- By Copying & Pasting
- Use Programming Libraries (Python, Java & JS)
- Using Professional XML to JSON Converter
Now, let us go through each method one by one in detail.
Quick Suggestion to Transform XML to JSON
Use the SysTools XML Converter to convert XML to JSON format including invalid, complex structured and deeply nested files, which can’t work with manual techniques. It’s an offline solution, which can handle your large files smoothly. It is far better than manual techniques, because it supports bulk conversion without taking long time. It provides 15+ different saving options including JSON format.
Simple Steps to Convert XML to JSON
- Step 1: Install the XML to JSON converter.
- Step 2: Upload your files/folders of XML.
- Step 3: Choose JSON as the saving type.
- Step 4: Select the path as the saving location.
- Step 5: Choose Next to convert XML to JSON.
Method 1: Edit Manually
If your XML file contains small and simple structure then manually XML to JSON conversion quite easy and appropriate. Just change XML elements, attributes, and text into respective JSON objects, arrays, and values.
XML Example:
<book id="123">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
</book>
Manually Conversion:
{
"book": {
"id": "123",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925
}
}
Note: In JSON, numbers (like ‘year’) are typically represented as actual numbers, not strings.
This method is highly prone to errors and impractical for anything beyond trivial examples.
Method 2: Use Programming Libraries (For Technical Users)
As highly effective options for conversion from XML to JSON, strong programming libraries should be recommended and acknowledged for the automated conversion-then-automated phase. Let us take examples of well-known languages:
I. Python
In Python, there is a complete array of libraries to tackle XML and JSON. xmltodict is the most commonly used one, making it a very straightforward conversion.
First, install xmltodict:
pip install xmltodict
Python Code Example:
import xmltodict
import json
xml_string = """
<library>
<book id="123">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<genre>Fiction</genre>
</book>
<book id="124">
<title>1984</title>
<author>George Orwell</author>
<genre>Dystopian</genre>
</book>
</library>
"""
# Convert XML to Python dictionary
data_dict = xmltodict.parse(xml_string)
# Convert Python dictionary to JSON string
json_string = json.dumps(data_dict, indent=4) # indent for pretty-printing
print(json_string)
Output:
{
"library": {
"book": [
{
"@id": "123",
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Fiction"
},
{
"@id": "124",
"title": "1984",
"author": "George Orwell",
"genre": "Dystopian"
}
]
}
}
Notice how xmltodict uses the @-prefix to denote attributes (like id) and repeats <book> elements as JSON arrays.
II. Java
In Java, libraries such as org.json (json-java) and jacksonare popularly used for transforming XML to JSON.
Using org.json:
First, add the json-20240303.jar (the latest version) to your library path in your project or, if you are using Maven, add this dependency in your XML file:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
Java Code Example (using org.json):
import org.json.JSONObject;
import org.json.XML;
public class XmlToJsonConverter {
public static void main(String[] args) {
String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<note>\n" +
" <to>Tove</to>\n" +
" <from>Jani</from>\n" +
" <heading>Reminder</heading>\n" +
" <body>Don't forget me this weekend!</body>\n" +
"</note>";
try {
JSONObject jsonObject = XML.toJSONObject(xmlString);
System.out.println(jsonObject.toString(4)); // 4 for pretty-printing
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
{
"note": {
"to": "Tove",
"from": "Jani",
"heading": "Reminder",
"body": "Don't forget me this weekend!"
}
}
III. JavaScript
Users often opt for xml-js and xml2js When they use JavaScript to convert XML file to JSON format. This is especially the case when an application runs under Node.js or in a browser environment. Below is an example of the code:
Using xml-js (Node.js):
First, install xml-js:
npm install xml-js
JavaScript Code Example (using xml-js):
const convert = require('xml-js');
const xmlData = `
<data>
<item id="A1">Value 1</item>
<item id="B2">Value 2</item>
</data>
`;
// Convert XML to JSON with compact format and 2 spaces for indentation
const jsonResult = convert.xml2json(xmlData, { compact: true, spaces: 2 });
console.log(jsonResult);
Output:
{
"data": {
"item": [
{
"_attributes": {
"id": "A1"
},
"_text": "Value 1"
},
{
"_attributes": {
"id": "B2"
},
"_text": "Value 2"
}
]
}
}
Note: Use
compact: trueto bring about the designation of XML attributes as_attributesand element text contents as_text.
Method 3: Offline XML to JSON Converter
As we suggest, if you are a non-tech savvy user, and have large, nested and complex structured XML files, which can’t be transform with manual methods, then use the mentioned software to convert XML to JSON. It’s a highly recommended solution to convert multiple XML files to JSON format. It retains the structure and elements during the process. Let’s learn more about the solution.
How to Transform XML to JSON Format? Tool’s Steps
- Download and start the solution on MacOS and Windows.

- Use the “Add Files” or “Add Folders” option to import the XML data you wish to convert.

- From the available export options, choose “JSON” as the target format.

- Select the desired destination folder for the converted JSON files.
- Click the “Export” button to start the process to convert XML to JSON.

- Once the conversion is complete, navigate to the output folder and verify the generated JSON files.

Why XML to JSON Conversion Tool?
- Multi-File Format Conversion: Converts XML into formats other than JSON, such as XML to TXT, HTML, DOC, SQL Script, MDB, ACCDB, DBF, CSV, and XLSX.
- Batch Conversion: Converts multiple XML files or entire XML folders at one go, enabling time-saving for extensive datasets.
- Preserves XML Structure: Converts XML data in such a way that the original hierarchy, elements, and formatting remain intact.
- User-Friendly Interface: Simple and interactive interface with two options for adding XML files. Add XML files one by one or add an entire folder at once.
- Offline Functionality: This software works as a desktop application where all conversions are performed locally. It gives an upper hand in terms of data security as compared to online tools to convert XML to JSON.
- Cross-Platform Availability: Available for Windows and MAC operating systems with support for various versions.
Wrapping It Up
Converting XML to JSON format is not an easy task, especially for non-technical users. So, we have explained best techniques for smooth process like edit codes manually, using programming libraries and an automated solution. First technique is best for small XML file; coding language is helpful for technical users.
And in case, you don’t have technical knowledge or have multiple large and nested data files then use the automated solution to convert XML to JSON. This software provides all possible features for smooth XML to JSON conversion. For any query, you can connect with us through live chat support.