JSON in Java has some great resources.

Maven dependency:

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20180813</version>
</dependency>

XML.java is the class you're looking for:

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

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);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException je) {
            System.out.println(je.toString());
        }
    }
}

Output is:

{"test": {
    "attrib": "moretest",
    "content": "Turn this to JSON"
}}
Answer from danieltalsky on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ convert xml to json using jackson
Convert XML to JSON Using Jackson | Baeldung
January 8, 2024 - Data binding, simply put, is when we want to map serialized data directly to a Java object. To explore this, letโ€™s define our XML with Flower and Color properties: <Flower> <name>Poppy</name> <color>RED</color> <petals>9</petals> </Flower> ... public class Flower { private String name; private Color color; private Integer petals; // getters and setters } public enum Color { PINK, BLUE, YELLOW, RED; } Our first step will be to parse the XML into a Flower instance.
Discussions

Convert xml to json with Java - Stack Overflow
Is there a way to convert a xml file to json? The XML can be of any structure, therefore there is no POJO class for instantiation. I need to convert the xml to json or to a Map, without root nodes.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting JSON to XML in Java - Stack Overflow
I guess the only way will be to deserialize the JSON to a (custom) Java Object. Then, using a framework like XMLBeans or XStream and with the help of annotations, specify which property goes as element and which goes as attribute 2013-11-14T14:27:40.423Z+00:00 More on stackoverflow.com
๐ŸŒ stackoverflow.com
Parse large XML file to JSON files?
60GB XML file holy crap More on reddit.com
๐ŸŒ r/aws
14
5
April 26, 2023
How to parse a XML file into a kotlin Data Class
๐ŸŒ r/Kotlin
14
17
February 28, 2019
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-xml-to-json-array-in-java
How to convert XML to JSON array in Java?
Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. To know more about Gson, refer to the Gson tutorial. We can use the Gson class of the Gson library to convert XML to JSON. The Gson class is a subclass of JsonElement class.
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ how-to-parse-json-xml-using-the-same-code-in-java-b3f79ac322c7
How To Parse JSON & XML Using The Same Code In Java | by ล.Gebel | The Startup
October 3, 2020 - In this article, youโ€™ll learn how to do it in Java by using dataformat-xml โ€” Jackson library extension in such a way that you only need to write your code once. Weโ€™ll go through the main features including model definition, custom serialization, and deserialization. Finally, Iโ€™ll share a tricky use-case where our โ€œone to rule them all codeโ€ wonโ€™t work and explain why.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ xml-to-json
Best XML to JSON Converter Online
XML to JSON is very unique tool for convert JOSN to XML and allows to download, save, share and print XML to JSON data..
๐ŸŒ
GitHub
github.com โ€บ digitalheir โ€บ java-xml-to-json
GitHub - cacfd3a/java-xml-to-json: ๐Ÿ‘พ convert XML to a structure-preserving JSON representation
This is a project to convert XML documents to JSON. It does this by taking a disciplined approach to create a terse JSON structure.
Starred by 16 users
Forked by 8 users
Languages ย  Java 100.0% | Java 100.0%
Find elsewhere
๐ŸŒ
Aspose
products.aspose.com โ€บ aspose.cells โ€บ java โ€บ conversion โ€บ xml to json
Java XML to JSON - XML to JSON Converter | products.aspose.com
November 13, 2025 - Add a library reference (import the library) to your Java project. Load XML file with an instance of Workbook class. Convert XML to JSON by calling Workbook.save method.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ xml to json java
How to Convert XML to JSON in Java | Delft Stack
March 11, 2025 - Learn how to convert XML to JSON in Java using popular libraries like org.json and Jackson. This comprehensive guide provides step-by-step code examples and detailed explanations to help you efficiently transform XML data into JSON format. Perfect for developers looking to enhance data interchange in their applications.
๐ŸŒ
InfoWorld
infoworld.com โ€บ home โ€บ software development โ€บ programming languages โ€บ java
Java XML and JSON: Document processing for Java SE, Part 1: SAXON and Jackson | InfoWorld
March 6, 2019 - java -cp jackson-annotations-2... Another way to convert from XML to JSON is to first parse the XML into a tree of JSON nodes and then write this tree to a JSON document....
๐ŸŒ
Code Beautify
codebeautify.org โ€บ xmltojson
XML to JSON Converter Online to convert XML to JSON String, URL and File
If you're looking for an easy way to convert XML to JSON, you've come to the right place. Our XML to JSON converter is free and easy to use, simply paste your XML code into the input and hit the "XML to JSON" button.
๐ŸŒ
Novixys Software
novixys.com โ€บ blog โ€บ convert-xml-json
Converting Between XML and JSON Using JAXB and Jackson | Novixys Software Dev Blog
December 12, 2017 - When you need to convert XML to JSON and back, you can use JAXB with Jackson for the conversion. JAXB converts XML to java objects, and Jackson converts the same java objects to JSON.
๐ŸŒ
InterSystems
community.intersystems.com โ€บ post โ€บ convert-xml-json
Convert XML to JSON | InterSystems Developer Community | Java|JSON|XML
Could you please submit java source files too? 1 0 https://community.intersystems.com/post/convert-xml-json#comment-125266 ... Method XML2JSON(iXml As %String) [ Language = python ] { import xmltodict, json #; print(iXml) obj = xmltodict.parse(iXml) return(json.dumps(obj)) }
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ jackson โ€บ jackson โ€“ converting between xml and json
Jackson - Converting between XML and JSON
August 30, 2022 - Learn to convert between JSON and XML strings using Jackson's JsonMapper and XmlMapper using simple POJO, JsonNode and ObjectNode classes.
๐ŸŒ
IBM
ibm.com โ€บ support โ€บ pages โ€บ how-transform-xml-json-java-compute-node
How to transform XML to JSON in Java Compute Node
// Get the first book's name - "Steve Jobs" String book1Name = message.getRootElement().getFirstElementByPath("JSON/Data/Books/Item/Name").getValueAsString(); See KLhttps://odc.supportportal.com/link/portal/5377/31053/Article/27994/How-to-use-XPath-with-JSON-messages-in-Java-nodes // Get the second book's name - "Beautiful Whale" MbElement booksEle = message.getRootElement().getLastChild().getLastChild().getLastChild(); String book2Name = booksEle.getFirstChild().getNextSibling().getFirstChild().getValueAsString(); Transformation Sample Requirement All XML data is queried from a database, here is the database table Empid Department FirstName LastName 1 Accounts John Doe 2 Accounts Ram Kumar 3 Accounts Joe Foster 4 Risk John Rowe 5 Risk Rob Conley 6 IT David Dickson 7 IT James Clear 8 IT Phil Viktor 9 IT Alfred Rich The goal is to transform XML data to a JSON format as below.
๐ŸŒ
Quora
quora.com โ€บ How-can-I-convert-XML-to-JSON-in-Java
How to convert XML to JSON in Java - Quora
Answer (1 of 3): *If you are converting this in a mmaven project add the org.json dependency to your pom.xml. ** You can add Jackson jars too your Java project to perform this conversion. Hope the above points helped you.
๐ŸŒ
Nile Bits
nilebits.com โ€บ home โ€บ blog โ€บ technical โ€บ engineering
How To Convert JSON Into XML Using Java? | Nile Bits
January 30, 2023 - Yes, we can convert XML to JSON in Java. There are many ways to do this, but one popular way is to use the Jackson library. Jackson is a powerful library for parsing and generating JSON.
๐ŸŒ
GitHub
github.com โ€บ mohapatra-sambit โ€บ xml-to-json-converter
GitHub - mohapatra-sambit/xml-to-json-converter: Java-based XML to JSON Converter โ€“ A Generic and Flexible Tool for Converting XML Data into JSON Format with Ease
Generic Conversion: Supports converting any XML structure to any JSON based on a provided schema. Command-Line Interface (CLI): Can be used directly from the command line for quick conversions. Library Integration: Easily integrate the converter into Java projects as a library dependency. Schema-Based Parsing: The application parses the provided JSON schema to map XML elements and attributes into the corresponding JSON structure.
Author ย  mohapatra-sambit
๐ŸŒ
Integrate.io
integrate.io โ€บ home โ€บ blog โ€บ conversions โ€บ how to convert xml to json: a step-by-step guide
How to Convert XML to JSON: A Step-by-Step Guide | Integrate.io
July 21, 2025 - This blog post details the step-by-step process of converting XML to JSON. Learn essential concepts, expert tips, and strategies through clear instructions and guidance for an effective conversion.