Search code examples
javajsonparsingpojojackson-databind

Need help parsing this JSON


I have this JSON file in following format. The first object is customer information, the second is order information, followed by n number of objects for details of each part ordered. I need to convert this to Java POJOs.

This is what JSON looks like. It's from external vendor, so I have to work with it as it is.

{
    "report_title": "title",
    "result_date": "08/12/2020 22:40:51",
    "company": "company name",
    "add1": "123 main st",
    "add2": null,
    "city": "some city",
    "state": "state",
    "country": "United States of America",
    "zip": "12345",
    "name": "first last",
    "username": "firstlast",
    "role": "Manager"
},
{
    "launch_date": "08/12/2020 19:05:19",
    "client": "client1",
    "order_number": "1",
    "total_orders": "1",
    "type": "type",
    "status": "Finished",
    "reference": "order reference",
    "fulfill": "some name",
    "duration": "00:07:12",
    "order title": "title",
    "order_groups": null,
    "part_nums": "1,2,3",
  },
  {
  "part_num": "1234",
  "name": "asdf",
  "other_name": "asdf",
  "manufacturer": "aasdf",
  "status": "asdfasf",
  "id": 12345,
  "title": "some title",
  "type": "Ig",
  "notes": "long winded notes",
  "impact": null,
  "solution": null,
  },
  {
  "part_num": "1235",
  "name": "asdf1",
  "other_name": "asdf1",
  "manufacturer": "aasdf1",
  "status": "asdfasf1",
  "id": 12346,
  "title": "some title",
  "type": "Ig",
  "notes": "long winded notes",
  "impact": null,
  "solution": null,
  }
...

I created four classes: Customer, OrderInfo, OrderDetail and encompassing class Order.

class Order {
    Customer customer
    OrderInfo orderInfo;
    List<OrderDetail> orderDetailList;
}

I am trying to use ObjectMapper, but I only ever get the customer object and nothing else. Any help appreciated.

EDIT: Here is what I tried

public static void main (String [] args) {
      File inputFile = new File(filepath);
       String content = Files.readString(Path.of(inputFile.getPath()), StandardCharsets.US_ASCII);

       ObjectMapper mapper = new ObjectMapper();
      
        Customer customer = mapper.readValue(content, Customer.class);
        OrderInfo orderInfo = mapper.readValue(content, OrderInfo.class);
}


@Data
@NoArgsConstructor
@JsonIgnoreProperties( ignoreUnknown = true )
public class Customer {
    @JsonProperty("report_title") private String reportTitle;
    @JsonProperty("result_date") private String resultDate;
    @JsonProperty("company") private String company;
...
}


Solution

  • I guess I posted the question little too early.

    I took Ajay Kr Choudhary's suggestion and just parsed each node separately and mapped them separately. Not as efficient or automated as I wanted, but still it's better than nothing.

     JsonNode rootNode = mapper.readTree(inputFile);
     Customer customer = mapper.treeToValue(rootNode.get(0), Customer.class);
     OrderInfo orderInfo = mapper.treeToValue(rootNode.get(1), OrderInfo.class);
     OrderDetail orderDetail = mapper.treeToValue(rootNode.get(2), OrderDetail.class);