Search code examples
javajsonpojo

Convert json string response to pojo


I am calling an API using rest template like below:

ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);

And here is the json response string that i receive from the API

{
    "data": {
        "individuals": [
            {
                "cust_xref_id": "abf",
                "cust_frd_alrt_in": "n",
                "cust_satis_trd_ct": "4",
                "gam_open_rv_trd_ct": "4",
                "cust_extnl_delinq_90_day_ct": "1",
                "cust_extnl_delinq_in": "y"
            }
        ]
    }
}

how can i map this response into a pojo? please help.


Solution

  • Required classes for the conversion are below,

    1. DataDTO

    public class DataDTO {
    private IndividualList data;
    
    public IndividualList getData() {
        return data;
    }
    
    public void setData(IndividualList data) {
        this.data = data;
    }}
    

    2. IndividualList

    public class IndividualList {
    
    private List<IndividualDTO> individuals;
    
    public List<IndividualDTO> getIndividuals() {
        return individuals;
    }
    
    public void setIndividuals(List<IndividualDTO> individuals) {
        this.individuals = individuals;
    }}
    

    3. IndividualDTO

    public class IndividualDTO {
    
    @JsonProperty("cust_xref_id")
    private String custXrefId;
    @JsonProperty("cust_frd_alrt_in")
    private String custFrdAlrtIn;
    @JsonProperty("cust_satis_trd_ct")
    private String custSatisTrdCt;
    @JsonProperty("gam_open_rv_trd_ct")
    private String gamOpenRvTrdCt;
    
    @JsonProperty("cust_extnl_delinq_90_day_ct")
    private String custExtnlDelinq90DayCt;
    @JsonProperty("cust_extnl_delinq_in")
    private String custExtnlDelinqIn;
    
    public String getCustXrefId() {
        return custXrefId;
    }
    
    public void setCustXrefId(String custXrefId) {
        this.custXrefId = custXrefId;
    }
    
    public String getCustFrdAlrtIn() {
        return custFrdAlrtIn;
    }
    
    public void setCustFrdAlrtIn(String custFrdAlrtIn) {
        this.custFrdAlrtIn = custFrdAlrtIn;
    }
    
    public String getCustSatisTrdCt() {
        return custSatisTrdCt;
    }
    
    public void setCustSatisTrdCt(String custSatisTrdCt) {
        this.custSatisTrdCt = custSatisTrdCt;
    }
    
    public String getGamOpenRvTrdCt() {
        return gamOpenRvTrdCt;
    }
    
    public void setGamOpenRvTrdCt(String gamOpenRvTrdCt) {
        this.gamOpenRvTrdCt = gamOpenRvTrdCt;
    }
    
    public String getCustExtnlDelinq90DayCt() {
        return custExtnlDelinq90DayCt;
    }
    
    public void setCustExtnlDelinq90DayCt(String custExtnlDelinq90DayCt) {
        this.custExtnlDelinq90DayCt = custExtnlDelinq90DayCt;
    }
    
    
    public String getCustExtnlDelinqIn() {
        return custExtnlDelinqIn;
    }
    
    public void setCustExtnlDelinqIn(String custExtnlDelinqIn) {
        this.custExtnlDelinqIn = custExtnlDelinqIn;
    }}
    

    Tested Response:

    {"data":{"individuals":[{"cust_xref_id":"abf","cust_frd_alrt_in":"n","cust_satis_trd_ct":"4","gam_open_rv_trd_ct":"4","cust_extnl_delinq_90_day_ct":"1","cust_extnl_delinq_in":"y"}]}}