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
Convert JSON to XML in Java
Just pass the given json object in XML.toString() method to convert it into xml data.
package com; import org.json.JSONObject; import org.json.XML; public class JSONToXML { public static void main(String...s){ String json_data = "{\"student\":{\"name\":\"Neeraj Mishra\", \"age\":\"22\"}}"; JSONObject obj = new JSONObject(json_data); //converting json to xml String xml_data = XML.toString(obj); System.out.println(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.
package com; import org.json.JSONObject; import org.json.XML; public class XMLToJSON { public static void main(String...s){ String xml_data = "<student><name>Neeraj Mishra</name><age>22</age></student>"; //converting xml to json JSONObject obj = XML.toJSONObject(xml_data); System.out.println(obj.toString()); } }
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.
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” : “john.adams@mma.edu”,
“grp” : “EMAIL”,
“pidm” : “”,
“type” : “Academy Email”
}, {
“address” : “wolfbat4813@gmail.com”,
“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!!!
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.
Underscore-java library has methods xmlToJson and jsonToXml.
Tutorial saved my day.
Hi Arun,
Were you able to change JSON To XMl keeping order intact ? Please help me her.
Yes, it works with underscore-java library.
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
How to remove {“soapenv:Envelope”:{“xmlns:xsd”:”http://www.w3.org/2001/XMLSchema”, tags while converting XML to JSON?
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
please how I can convert XML to JSON without instance this line
String xml_data = “Neeraj Mishra22”;
thanks for help
HELLO !!
how I can convert XML to JSON without instance the string xml_data
thanks for any help
regards
Your question is not clear at all – which probably explains why noone has helped answer your question.
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