What is the quickest way to convert XML file to JSON file in Java?

Related searches

I currently have a bunch of XML files(each are 16kb in size) that need to be converted into JSON and then written to new files. I have a working implementation but it's very slow.

What I'm doing is grabbing each file in the directory, converting the XML data to a string, creating a JSON object of that String with org.json, then using FileWriter to write it to a file

import org.json.JSONObject;
import org.json.XML;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.apache.commons.io.FilenameUtils;

public class TestConvert {

    public static void main(String[] args) {

        final File xmlFolder = new File("C:\\files\\xml_files");
        final File jsonFolder = new File("C:\\files\\json_files");

        for(File fileEntry: xmlFolder.listFiles()){
            try {
                String xml = new String(Files.readAllBytes(fileEntry.toPath()), StandardCharsets.UTF_8);
                JSONObject obj = XML.toJSONObject(xml);

                String completeDir = jsonFolder + "\\" + FilenameUtils.removeExtension(fileEntry.getName()) + ".json";

                FileWriter file = new FileWriter(completeDir);
            } catch (IOException e) {

This does work, however I'm currently testing this with ~370k XML files, and the running time is a shade over 45 minutes. This seems excessive, and I'd really like to cut down the length of time it takes to complete this.

You can use this: https://github.com/stleary/JSON-java


And Java file:

import org.json.JSONObject;
import org.json.XML;

public class Main {

    public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    public static String TEST_XML_STRING =
        "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

    public static void main(String[] args) {
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
        } catch (JSONException je) {

Quickest way to convert XML to JSON in Java, To convert XML File in to JSON include the following dependency. <dependency >. <groupId>org.json</groupId>. <artifactId>json</artifactId>. The only problem with JSON in Java is that if your XML has a single child, but is an array, it will convert it to an object instead of an array. This can cause problems if you dynamically always convert from XML to JSON, where if your example XML has only one element, you return an object, but if it has 2+, you return an array, which can cause parsing issues for people using the JSON.

You could have tried using a BufferedOutputStream, or the following. I used Files.list, as the tradition File.listFiles is slow for large directories.

    final Path xmlFolder = Paths.get("C:\\files\\xml_files");
    final Path jsonFolder = xmlFolder.resolveSibling("json_files");

        .forEach(path -> {
        try {
            String xml = new String(Files.readAllBytes(fileEntry.toPath()),

            // 1
            final int initialXmlSize = 320 * 1024;
            JSONObject obj = XML.toJSONObject(xml);
            StringWriter xmlOut = new StringWriter(initialXmlSize);
            String xml = xmlOut.toString();
            // 2

            String jsonFileName = path.getFileName().toString().replaceFirst("\\.[^\\.]+$",
                "") + ".json";
            Path xmlPath = jsonFolder.resolve(jsonFileName);
            Files.write(xmlPath, xml.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            System.err.println("File " + path);

The code between // 1 and // 2 could be optimized further to only use strings, no DOM objects (XM, JSON). XSLT would be one solution (though faster?).

Quickest way to convert XML to JSON in Java, In this quick article, we'll explore how to convert XML to JSON in Java. Java community didn't include any capability convert XML to JSON on standard JDK. Quickest way to convert XML to JSON in Java ; To convert XML File in to JSON include the following dependency <dependency> <groupId>org.json</groupId>

Underscore-java library has static method U.xmlToJson(xml). I am the maintainer of the project.

Convert XML to JSON in Java, Learn different ways in which Jackson can read XML data and write it to JSON. In this tutorial, we'll see how to convert an XML message to JSON Data binding , simply put, is when we want to map serialized data directly to a Java object. Our first step will be to parse the XML into a Flower instance. To convert XML File in to JSON include the following dependency <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> and you can Download Jar from Maven Repository here .

Convert XML to JSON Using Jackson, Learn how to transform and convert XML and JSON documents with you should have downloaded the following JAR files from the Maven repository: Once the object has been created, it's easy to write it out as JSON by� I would like to share converting XML into JSON format. XML: XML (Extensible Markup Language) is a mark-up language that defines the set of rules for encoding document in Human &Machine readable format. XML mainly emphasizes on Simplicity, Generality and Usability over the Internet.

Java XML and JSON: Document processing for Java SE, Part 1 , XML to JSON Converter Online This is a free online XML to JSON converter using which you can convert your XML files to JSON files quickly and easily. The process of conversion takes a few seconds. Once completed, you can store the converted file by downloading it to your system.

In the tutorial, we show how to convert Json to XML and vice versa with Java language by examples. ContentsJson dependencyConvert Json to XMLConvert Json String to XML StringConvert Json File to XML fileFull ProgramConvert XML to JsonConvert XML String to Json StringConvert XML File to Json FileFull Program Json dependency For convert Json to/from …

  • You could use multi-threading to complete the process in even lesser time. Spawn numbers of threads and give each thread a number of xml files.
  • How big are your XML files? How many elements and depth? This could be an amazingly fast time for all we know!
  • That's a good point, Jamie. Thank you. Each file size is 16kb. I believe it goes to a depth of 10 or 11.
  • Have you considered xslt?
  • don't know if it will be faster or not, but give jackson library a try, this, and use a threadpool and split different files across different threads.