How to deserialize a field in nested array

Viewed 212

I am having problem getting hold of one of the field that is in nested array.

First i am trying to iterate through root array of my json like this -

                for(SearchHit h : hits){
                    String source = h.getSourceAsString().replace("null","\"\"");
                    
                    TestResponse svcResp = mapper.readValue(source, TestResponse.class);
                    dataResp.add(svcResp);
                }

Now the source json would look like this -

 {

  "name": [
    {
      "family": "Doe",
      "given": "Jon",
      "middle": "Smith",
      "use": "Commercial"
    }
  ]
}

and my pojo would look like this -

private String fstNm;
List<Map<String,Object>> name=null;
private List<Map<String,Object>> name;

public String getFstNm() { return fstNm; }


public void setFstNm(List<Map<String, Object>> hits) {

      name = (List) hits.get(0).get("name");

      this.fstNm= (String) name.get(0).get("given");
        

}

My Expected Output -

{
    "fstNm":"Jon"
}

But i am getting actual Output -

{
    "fstNm":null,
    "name": [
          {
            "family": "Doe",
            "given": "Jon",
            "middle": "Smith",
            "use": "Commercial"
          }
        ]

   }

How do i get my expected output to be working ?

Update Posting my entire pojo -

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import java.util.Map;
import java.util.List;

@AllArgsConstructor
@Setter
@Getter
public class TestResponse {

    @JsonProperty("dummyID")
    @JsonAlias("dummyID")
    private String dummyID;

 


    private String fstNm;


    private List<Map<String, Object>> name;

    public String getFstNm() {
        return fstNm;
    }

    public void setFstNm(String fstNm) {
        this.fstNm=fstNm;

    }


    public List<Map<String, Object>> getName() {
        return name;
    }

    public void setName(List<Map<String, Object>> name) {
        this.name = name;
        this.fstNm= (String) name.get(0).get("given");
    }

    public String getdummyId() {
        return dummyId;
    }

    public void setdummyId(String dummyId) {
        this.dummyId = dummyId;
    }
}
    
2 Answers

You could simply set fstName inside the setName method as name property will get parsed.

Not commenting whether you are doing it right or wrong. Just posting a simple solution that will work and of course there are many other ways of achieving it like custom deserializer etc.

public class Test {

    @org.junit.Test
    public void test() throws JsonMappingException, JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        String input = "{\r\n" + "\r\n" + "  \"name\": [\r\n" + "    {\r\n" + "      \"family\": \"Doe\",\r\n"
                + "      \"given\": \"Jon\",\r\n" + "      \"middle\": \"Smith\",\r\n"
                + "      \"use\": \"Commercial\"\r\n" + "    }\r\n" + "  ]\r\n" + "}";
        System.out.println(input);

        System.out.println(mapper.readValue(input, TestResponse.class));
    }
    
    @JsonIgnoreProperties(ignoreUnknown = false)
    @JsonPropertyOrder({"name", "fstNm" })
    protected static class TestResponse {

        private String fstNm;
        
        
        private List<Map<String, Object>> name;

        public String getFstNm() {
            return fstNm;
        }

        public void setFstNm(String fstNm) {
            this.fstNm=fstNm;

        }       
        

        public List<Map<String, Object>> getName() {
            return name;
        }

        public void setName(List<Map<String, Object>> name) {
            this.name = name;
            this.fstNm= (String) name.get(0).get("given");
        }

        @Override
        public String toString() {
            return "Pojo [fstNm=" + fstNm + ", name=" + name + "]";
        }
        
        

    }
}

Output:

{

  "name": [
    {
      "family": "Doe",
      "given": "Jon",
      "middle": "Smith",
      "use": "Commercial"
    }
  ]
}
Pojo [fstNm=Jon, name=[{family=Doe, given=Jon, middle=Smith, use=Commercial}]]

Just a suggestion: why don't you use Map<String, Object> source = h.getSourceAsMap(); instead of parsing the JSON with the Mapper?

Related