Convert JSON to XML using Java Jackson Library

Post Banner

In order to convert a JSON String into XML we will need to make use of the two Jackson Libraries,

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.4'


We will first convert the JSON String to Java Object using ObjectMapper, then to XML String using XmlMapper class,

Employee POJO:
public class Employee {

    String empName;
    int empAge;
    String empDept;

    public String getEmpName() {
        return empName;

    public void setEmpName(String empName) {
        this.empName = empName;

    public int getEmpAge() {
        return empAge;

    public void setEmpAge(int empAge) {
        this.empAge = empAge;

    public String getEmpDept() {
        return empDept;

    public void setEmpDept(String empDept) {
        this.empDept = empDept;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;


public class JavaJsonToXML {

    public static void main(String[] args) {
        try {

            //Step 1: JSON String
            String jsonString = "{\"empName\":\"Andy\",\"empAge\":22,\"empDept\":\"Insurance\"}";
            ObjectMapper objectMapper = new ObjectMapper();

            //Step 2: JSON String to Java Object
            Employee employee = objectMapper.readValue(jsonString, Employee.class);

            XmlMapper xmlMapper = new XmlMapper();

            //3. JavaObject to XML String
            String xmlString = xmlMapper.writeValueAsString(employee);

        } catch (IOException e) {
        } catch (Exception e) {


Have Questions? Post them here!
Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.