0

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.

Tom H
  • 3
  • 1

2 Answers2

0

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
Clark Ngo
  • 343
  • 4
  • 14
-1

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


}
len kim
  • 1
  • 2
  • I edited the code as u showed but still returns null – Tom H Sep 05 '22 at 00:46
  • NoArgsConstructor can help for instantiation objects without parameters. But in this case, we can assume the poster has parameters when objects are created. – Clark Ngo Sep 05 '22 at 00:50