Java XML to JSON: Jackson, org.json, and Gson Examples
- Jackson's XmlMapper converts XML to JSON in one method call — the most common Java approach.
- org.json's XML class provides a simpler alternative without the Jackson dependency.
- Both produce standard JSON output with attribute and element mapping.
Table of Contents
Java has several established libraries for XML-to-JSON conversion, each with different trade-offs. Jackson's XmlMapper is the most commonly used in enterprise Java applications. org.json's XML class is simpler with fewer dependencies. This guide covers both with Maven/Gradle dependency declarations and complete working examples.
Jackson XmlMapper: The Enterprise Standard
Jackson is the most widely-used JSON library in Java. The jackson-dataformat-xml module adds XML support. Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.16.1</version>
</dependency>
Convert XML string to JSON string:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xmlString.getBytes());
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
Jackson handles nested elements, arrays, and basic attribute mapping. For files, replace xmlString.getBytes() with a File or InputStream reference.
org.json: Simpler XML to JSON
org.json's XML utility class converts XML to JSONObject with a single method call. Maven dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
import org.json.JSONObject;
import org.json.XML;
JSONObject json = XML.toJSONObject(xmlString);
System.out.println(json.toString(2)); // 2 = indent spaces
org.json handles most standard XML structures. XML attributes are included in the JSON output alongside element children. Repeated elements may be treated as arrays depending on the XML structure.
Handling XML Attributes in Java JSON Output
XML attributes require special handling in Java converters. With Jackson's XmlMapper, attributes are mapped to keys with a leading underscore or the configured prefix, depending on your configuration:
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, false);
For custom attribute naming in Jackson, use the @JacksonXmlProperty annotation when mapping to POJOs, or configure a custom DeserializationFeature.
With org.json, attributes are handled automatically — they appear as sibling keys in the JSON object alongside child element keys. A content key is created for the element's text value when attributes are present.
For quick manual XML inspection without the complexity of library configuration, the browser converter handles attribute mapping (@key convention) automatically without any setup.
Quick Inspection Without Code
Paste XML, get JSON. No Maven, no classpath, no compile step.
Open Free XML to JSON ConverterFrequently Asked Questions
Which Java library should I use for XML to JSON — Jackson or org.json?
Jackson XmlMapper if your project already uses Jackson (common in Spring Boot apps). org.json if you want minimal dependencies and simple conversion without Jackson's configuration overhead.
Does Jackson handle SOAP XML?
Yes. Jackson's XmlMapper processes any well-formed XML including SOAP envelopes. Namespace prefixes appear in key names. Use a post-processing step to clean namespace-qualified key names if needed.
Can I convert XML to a specific Java class (POJO) instead of JSON?
Yes — Jackson's XmlMapper.readValue(xmlString, MyClass.class) maps directly to a POJO if your class annotations match the XML structure. JSON output is not needed for this use case.
What's the fastest Java XML parser for large files?
StAX (javax.xml.stream) is the fastest for large files — it's a streaming parser that doesn't load the entire document into memory. Jackson internally uses StAX for its XML processing.

