DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert CSV to XML in Java
  • Dataweave Interview Question Using Map and Reduce
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • How to Convert XLS to XLSX in Java

Trending

  • Vision-Language Models in 2026: From Research Breakthroughs to Cloud-Native Production Systems
  • Part 2: Securing and Scaling Goose-to-Java Agent Traffic With agentgateway
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. How to Convert JSON to XML or XML to JSON in Java

How to Convert JSON to XML or XML to JSON in Java

Learn how to easily convert a JSON object to XML or XML to JSON using APIs in Java.

By 
Brian O'Neill
DZone Core CORE ·
Jun. 24, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Understanding how to convert between complex file types will allow you to optimize and perfect your operations, ensuring your data is always in the appropriate form. The two formats featured in today’s tutorial are JSON and XML. Both formats are widely used in web-based applications, but one may be more ideal than the other for certain objects. JavaScript Object Notation (JSON) is defined as an open standard file and data-interchange format that uses human-readable text to store and exchange data. It was initially developed from JavaScript, but as a language-independent format it can be used in many different programming languages. Due to its versatility, efficient design, and low cost, JSON has become a go-to for many businesses. 

Our other format, Extensible Markup Language (XML), is similar to JSON in that they both are easy to read, create, and decode. However, XML uses prescribed tags that divide information according to its traits and has limiting factors associated with characters (i.e. !”*(), etc.). While this option may contain a higher word count due to the tags, it does allow for more precision in how data is read by the computer, including improved metadata usability, which is unavailable in JSON. 

So, what does all this mean in terms of which option you should use? The answer is: it depends on the type of project you are working with. If it is a browser-based project, JSON is generally the best choice, while XML is best suited for server-based projects. No matter which format you choose, we have you covered with an easy way to move between the two. By utilizing the following APIs in Java, you will be able to automatically convert JSON to XML or XML to JSON with very little effort on your end.

To begin the process, we will install Maven by adding a reference to the repository in pom.xml:

Java
 
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>


Then, we will add a reference to the dependency:

Java
 
<dependencies>
<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v3.90</version>
</dependency>
</dependencies>


Once the installation is complete, we are ready to call our first conversion function, which will be converting from JSON to XML:

Java
 
// 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)
//Apikey.setApiKeyPrefix("Token");

ConvertDataApi apiInstance = new ConvertDataApi();
Object jsonObject = null; // Object | Input JSON to convert to XML
try {
    byte[] result = apiInstance.convertDataJsonToXml(jsonObject);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling ConvertDataApi#convertDataJsonToXml");
    e.printStackTrace();
}


To ensure the operation is successful, you will need to input the following parameters:

  • JSON Object – the JSON object you wish to convert.
  • API Key – your personal API key. If you need to obtain a personal API key, you can do so by register for a free account on the Cloudmersive website; this will provide 800 monthly calls across any of our APIs.

Alternatively, if you need to convert XML to JSON, you simply need to input your target XML file and API key into the following code:

Java
 
// 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)
//Apikey.setApiKeyPrefix("Token");

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);
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling ConvertDataApi#convertDataXmlToJson");
    e.printStackTrace();
}


These tools will provide a quick and efficient way to move between these two popular data formats, ensuring you don’t miss a beat if you need to shift from one to the other.

JSON XML Java (programming language) Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert CSV to XML in Java
  • Dataweave Interview Question Using Map and Reduce
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • How to Convert XLS to XLSX in Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook