Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jackson JSON parsing returns null

I’m learning how to parse json file for API testing. But while I was parsing using jackson it returns null values where actual values are not null in JSON .
My JSON is :

    {
  "id": 100,
  "FName": "Rayan",
  "LName": "Philip",
  "Role": "Manager"
}

DataBinding class is the class that has code to parse JSON file., which is in Project/src/test/java.

DataBinding class is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import Models.Employee;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.testng.annotations.Test;


import java.io.File;
import java.io.IOException;

public class TestDataBinding {
    @Test
    public void TestDataBinding() throws IOException {
        ObjectMapper map = new ObjectMapper();
       map.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        Employee emp = map.readValue(new File("src/main/resources/Data/Employees.json"), Employee.class);
        System.out.println(emp.getFName());
        System.out.println(emp.getLName());


    }
}

Employee class has the key variable declarations and getter and setter methods.

Employee Class is:

package Models;

public class Employee {
    private int id;
    private String FName;
    private String LName;
    private String Role;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFName() {
        return FName;
    }

    public void setFName(String FName) {
        this.FName = FName;
    }

    public String getLName() {
        return LName;
    }

    public void setLName(String LName) {
        this.LName = LName;
    }

    public String getRole() {
        return Role;
    }

    public void setRole(String role) {
        Role = role;
    }


}

Output is:

null
null

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Please advice a way to parse it properly.

>Solution :

Add this to your Employee.class. Starting with a capital is not a Java naming conventions (variables should start with lower case letters). It will look for fName, hence we need to define FName explicitly.

import com.fasterxml.jackson.annotation.JsonProperty;

// some code

    @JsonProperty("FName")
    private String FName;


    @JsonProperty("LName")
    private String LName;

    @JsonProperty("Role")
    private String Role;

// some code
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading