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

In this tutorial you will learn how to convert json to xml or xml to json in java.

While programming we may come across a situation where we have to convert json to xml or vice versa. JSON.org provides a library that can be used for this conversion process in just one line of code.

Below I have shared examples to do this.

Download the json java library from below link and import in your project.

Link: https://mvnrepository.com/artifact/org.json/json

If you don’t know how to import a library in java then you can read below article.

Also Read: How to Add or Import Jar in Eclipse Project

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

Convert JSON to XML in Java

Just pass the given json object in XML.toString() method to convert it into xml data.

Output

<student><name>Neeraj Mishra</name><age>22</age></student>

Convert XML to JSON in Java

Just pass the xml data into XML.toJSONObject() method to convert it into json object.

Output

{“student”:{“name”:”Neeraj Mishra”,”age”:22}}

Programming Tip: Level up your programming skills by moving your software development/testing environment into the cloud with innovative hosted citrix vdi from CloudDeskopOnline with remote accessibility to essential programming tools on any device(PC/android/iOS). Looking for a dedicated server for your project? Try out dedicated gpu hosting and Enterprise E3 suite from Apps4Rent to gear up your work productivity.

13 thoughts on “How to Convert JSON to XML or XML to JSON in Java”

  1. Is there any way to maintain order while transforming JSON to XML. I have a Input JSON file. But when I convert it into XML, the order will be changed. I was searching solution from past 2 days. I tried in mule as well as in Java but it won’t work. This is the Input JSON data.
    {
    “status” : “success”,
    “response” : {
    “p_status_msg” : {
    “contact” : {
    “contactlist” : [{
    “address” : “90 Grove St., Newport ME 04953; United States of America”,
    “grp” : “ADDRESS”,
    “pidm” : “”,
    “type” : “Home (preferred)”
    }, {
    “address” : “[email protected]”,
    “grp” : “EMAIL”,
    “pidm” : “”,
    “type” : “Academy Email”
    }, {
    “address” : “[email protected]”,
    “grp” : “EMAIL”,
    “pidm” : “”,
    “type” : “Email”
    }, {
    “address” : “(207) 341-3018”,
    “grp” : “PHONE”,
    “pidm” : “”,
    “type” : “Cell Phone”
    }
    ],
    “contact” : {
    “studentid” : “P000020581”,
    “dateofbirth” : “07/07/1995”,
    “firstname” : “John”,
    “socialsecuritynumber” : “005961046”,
    “gender” : “M”,
    “middlename” : “Q.”,
    “lastname” : “Adams”
    }
    }
    }
    }
    }

    Please help me out. Thanks!!!

    1. LOLNishant LMAOKumar

      Yes you can maintain the order.
      Once the program gives you have the output result, copy and paste it a notepad and then cut paste the data to bring it in correct order.

        1. please how i can convert string XML to a JSON without instance
          String xml_data = “Neeraj Mishra22”;
          i want to convert any xml send to json
          thanks in advance

  2. How to remove {“soapenv:Envelope”:{“xmlns:xsd”:”http://www.w3.org/2001/XMLSchema”, tags while converting XML to JSON?

  3. please how I can Converter any XML send, instead of instantiating the XML string to Converter,
    how I can boost the conversion.
    I want to replace :
    String xml_data = “Neeraj Mishra22”
    to be dynamic
    thanks in advance for any help

  4. please how I can convert XML to JSON without instance this line
    String xml_data = “Neeraj Mishra22”;
    thanks for help

  5. Kishore Diyyana:

    For Spring Boot App to Ignore Case for JSON attributes names:

    Step 1: Make sure your POJO/Domain/Model class has constructor with respective parameters, remove any zero arg constructor. Step 2: Add Jackson lib as dependency Ex:

    com.fasterxml.jackson.module
    jackson-module-jaxb-annotations
    2.6.5

    Step 3: Enable in application.properties file as below spring.jackson.mapper.accept_case_insensitive_properties=true

Leave a Comment

Your email address will not be published. Required fields are marked *