How to Convert XML (string OR file format) to JSON using Java

2 min readJun 8, 2022


Converting XML to JSON is a common need for many developers. With a free Cloudmersive account (register on our website) & the help of our XML to JSON Conversion API, this conversion will become a fluid and easy task to complete on a regular basis (for both XML strings AND files).

The best part: you can easily take advantage of this API by following instructions below to structure your API call. We’ve provided ready-to-run code examples in Java for your convenience, but you can easily find additional code examples in 12 other common programming languages by visiting our API console page.

We’re going to begin by installing the Java SDK with Maven. To do so, first add the below reference to the repository in pom.xml:


Then add the below reference to the dependency:


Now you can include the remaining code block to complete the API call:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDataApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
ConvertDataApi apiInstance = new ConvertDataApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
Object result = apiInstance.convertDataXmlToJson(inputFile);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDataApi#convertDataXmlToJson");




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.