How To Convert JSON To XML In Java ?

In this article we will see how to convert JSON into XML in Java ? Here we will use Java-JSON library to convert JSON to XML.

First add below JSON dependency in your pom.xml
                <dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20201115</version>
		</dependency>

Now lets convert JSON into XML.
import org.json.JSONObject;
import org.json.XML;

public class JsonToXml {

	public static void main(String[] args) {
		
		String jsonStr = "{\r\n" + 
				"	employee : {\r\n" + 
				"		\"emp_id\":1,\r\n" + 
				"		\"name\":\"John\",\r\n" + 
				"		\"cmpName\":\"TATA\"\r\n" + 
				"	}\r\n" + 
				"}";
		
		JSONObject json = new JSONObject(jsonStr);
		
		System.out.println(XML.toString(json));
		
	}

}

0 Comments