1

Can someone please let me know if below JSON to POJO conversion is possible or not,

Sample JSON

{
 "city":"test",
 "firstname":"nokia",
 "lastname":"mobile"
}

Response Class

@Data
public class BRNResponse {

    @JsonProperty("city")
    public String city; 

    private Name name;

}

@Data
public class Name{
    @JsonProperty("firstname")
    public String firstName;    

    @JsonProperty("lastname")
    public String lastName; 

}

I am able to get city value but not the first name & last name, Please help me to sort out this

  • See [Jackson conditional @JsonUnwrapped](https://stackoverflow.com/questions/25425419/jackson-conditional-jsonunwrapped), [Wrapping Json fields into instance variable of a pojo](https://stackoverflow.com/questions/54370637/wrapping-json-fields-into-instance-variable-of-a-pojo) – Michał Ziober Sep 16 '19 at 21:25

1 Answers1

2

Since you are using Jackson, try this:

ublic class BRNResponse {

  @JsonProperty("city")
  public String city; 

  // Unwrap the name and place its members directly into BRBResponse.  
  @JsonUnwrapped
  private Name name;
}

See also https://www.baeldung.com/jackson-annotations