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) {


Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap