title: How to make POJO and parse recursive objects using jackson?
body: <p>I have a below JSON response that I am getting back from a rest service. Now I need to deserialize below JSON response into a POJO. I am working with Jackson.</p>
  
  <pre><code>{
      "pagination": {
          "number": 1,
          "entriesPerPage": 200,
          "total": 3
      },
      "postings": [{
          "categories": [{
          "taskid": "79720",
          "name": "Sunglasses",
          "parentCategory": {
              "taskid": "394",
              "name": "Sunglasses &amp; Fashion Eyewear",
              "parentCategory": {
                  "taskid": "2340",
                  "name": "Men's Accessories",
                  "parentCategory": {
                      "taskid": "10987",
                      "name": "Clothing, Shoes &amp; Accessories"
                  }
              }
          }
      }]
  },
  {
      "categories": [{
          "taskid": "12980",
          "name": "Toys",
          "parentCategory": {
              "taskid": "123",
              "name": "Fashion",
              "parentCategory": {
                  "taskid": "78765",
                  "name": "Men's Accessories"
              }
          }
      }]
  }],
      "total": 2
  }
  </code></pre>
  
  <p>In above json, <code>postings</code> is a JSON array which can have multiple <code>posting</code> json object. Now <code>categories</code> is also JSON array. Now tricky part is I can have multiple levels of <code>parentCategory</code> inside each category object and I don't know how many levels of <code>parentCategory</code> I will have. Give above JSON, I need to extract <code>taskid</code> of each category and <code>taskId</code> of last <code>parentCategory</code>. So it should be like this:</p>
  
  <pre><code>79720=10987
  12980=78765
  </code></pre>
  
  <p>Where <code>79720</code> is taskId of category and <code>10987</code> is the taskId of last <code>parentCategory</code>. Similarly for other one.</p>
  
  <p>Below is my code where I am deserializing JSON into my POJO by making an http call:</p>
  
  <pre><code>ResponseEntity&lt;Stuff&gt; responseEntity = HttpClient.getInstance().getClient()
      .exchange(URI.create(endpoint), HttpMethod.POST, requestEntity, Stuff.class);
  Stuff response = responseEntity.getBody();
  
  List&lt;Posting&gt; postings = response.getPostings();
  for(Posting postings : postings) {
      //....
  }
  </code></pre>
  
  <p>Confusion I have is - How to make POJO for above JSON? I tried using <a href="http://www.jsonschema2pojo.org/" rel="nofollow noreferrer">jsonschema2pojo</a> but it's not making right classes for <code>parentCategory</code>. Since I can have nested levels of parentCategory which I don't know before hand.</p>
  
  <p>Is this possible to do using Jackson?</p>
  
  <p>This is the POJO class generated for <code>Category</code> and <code>ParentCategory</code>. I am not sure whether I need to make any changes here so that I can parse recursive <code>parentCategory</code> object.</p>
  
  <pre><code>@JsonInclude(JsonInclude.Include.NON_NULL)
  @JsonPropertyOrder({"taskid", "name", "parentCategory"})
  public class Category {
    @JsonProperty("taskid")
    private String taskid;
    @JsonProperty("name")
    private String name;
    @JsonProperty("parentCategory")
    private ParentCategory parentCategory;
    @JsonIgnore
    private Map&lt;String, Object&gt; additionalProperties = new HashMap&lt;String, Object&gt;();
  
    ...
  
  }
  
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @JsonPropertyOrder({"taskid", "name", "parentCategory"})
  public class ParentCategory {
    @JsonProperty("taskid")
    private String taskid;
    @JsonProperty("name")
    private String name;
    @JsonProperty("parentCategory")
    private ParentCategory parentCategory;
    @JsonIgnore
    private Map&lt;String, Object&gt; additionalProperties = new HashMap&lt;String, Object&gt;();
  
    ...
  
  }
  </code></pre>