Home > Enterprise >  Jackson JSON parsing returns null
Jackson JSON parsing returns null

Time:09-05

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:

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.

CodePudding user response:

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

CodePudding user response:

should have noArgsconstracture if you want to parse by jackson.

package Models;

public class Employee {
    private int id;
    private String FName;
    private String LName;
    private String Role;

    public Employee(){ } // Look at this!!

    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;
    }


}
  • Related