Home > Software design >  How to fix null values from converting JSON to POJO using Gson
How to fix null values from converting JSON to POJO using Gson

Time:11-28

I am trying to get a response message to a POJO class that has already been defined. However, I am getting null values for all the fields in the POJO class. I printed the json file and they are not null and I am wondering what is actually going. This is the response that I get.

I should get this VirtualAccountResponseDto(account_number=null, bank_code=null, unique_id=null, account_name=null, account_reference=null, bank_name=null, created_at=null, currency=null, id=null, account_status=null, customer=null)

This is the gson class that I printed to the console

>>>>>>>>>{"data":{"account_number":"1110006278","bank_code":"000","unique_id":"KPY-VA-NEBOrKvKCmTSOJe","account_name":"James Bond","account_reference":"xyz163ath285","bank_name":"test","created_at":"2022-11-27T23:38:36.449Z","currency":"NGN","id":6268,"account_status":"active","customer":{"name":"James Bond","email":"[email protected]"}},"message":"Virtual bank account created successfully","status":true}

This is what the POJO class looks like

@Data
@Builder

@AllArgsConstructor
@NoArgsConstructor
public class VirtualAccountResponseDto {
    @SerializedName("account_number")
    private String account_number;
    @SerializedName("bank_code")
    private String bank_code;
    private String unique_id;

    private String account_name;
    private String account_reference;

    private String bank_name;
    private String created_at;
    private String currency;
    private String id;
    private String account_status;
    private Customer customer;

This is the customer class

@Data
public class Customer {
  private String email;
  private String name;

}

Here is my code to convert from Json to POJO

Gson gson = new Gson();

        String requestJson = gson.toJson(bankDto);

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "Bearer " KORAPAY_TOKEN);
        headers.put("accept", "application/json");


        HttpResponse<JsonNode> apiResponse = Unirest.post(KORAPAY_BASE_URL  
                        "virtual-bank-account")
                .headers(headers)
                .body(requestJson)
                .asJson();
        System.out.println(apiResponse.getBody());
        JSONObject responseJson = apiResponse.getBody().getObject();
        System.out.println("This is the response body"   apiResponse.getBody());
        JsonParser jsonParser = new JsonParser();
        JsonObject gsonObject = (JsonObject)jsonParser.parse(responseJson.toString());
        System.out.println("         >>>>>>>>>"   gsonObject);

        VirtualAccountResponseDto responseDTO = gson.fromJson(gsonObject,
                VirtualAccountResponseDto.class);
        System.out.println("I should get this "   responseDTO);
        return responseDTO;

I have tried annotating the variable names with @Serializedname. I have also checked for case_senstivity issues. Also, I have rearranged the POJO fields in the order in which the JSON is returned and I still get the same null values

CodePudding user response:

As @Marcono1234 pointed out, your DTO structure and JSON structures do not match. I tried by modifying the structure of the DTO class to add a 'data' element and it works fine after that.

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VirtualAccountResponseDto {
    DataClass data; // <-- Added this class to match the JSON structure
}

@Data
class DataClass {
    private String account_number;
    private String bank_code;
    private String unique_id;

    private String account_name;
    private String account_reference;

    private String bank_name;
    private String created_at;
    private String currency;
    private String id;
    private String account_status;
    private Customer customer;
}

@Data
class Customer {
  private String email;
  private String name;
}

Output after the change:

VirtualAccountResponseDto(data=DataClass(account_number=1110006278, bank_code=000, unique_id=KPY-VA-NEBOrKvKCmTSOJe, account_name=James Bond, account_reference=xyz163ath285, bank_name=test, created_at=2022-11-27T23:38:36.449Z, currency=NGN, id=6268, account_status=active, customer=Customer([email protected], name=James Bond)))

  • Related