2

I want to parse the json string and form a pojo object but the response is somewhat unusual. I have folloing type of response from API

  "data": {
          "12": {
             "value": "$0.00",
             "order_id": "12",
             "order_date": "2020-08-26 15:50:05",
             "category_name": "Games",
             "brand_id": "4",
             "denomination_name": "AED 50",
             "order_quantity": "1",
             "vendor_order_id": "A-123",
             "vendor_location": "",
             "vouchers": {
                "804873": {
                   "pin_code": "41110AE",
                   "serial_number": "fddfgfgf1234444"
                }
             }
          },
          "15": {
             "value": "$0.00",
             "order_id": "15",
             "order_date": "2020-08-26 08:39:11",
             "category_name": "Games",
             "brand_id": "52",
             "brand_name": "PlayStation",
             "denomination_name": "$20",
             "order_quantity": "1",
             "vendor_order_id": "A-316",
             "vendor_location": "",
             "vouchers": {
                "806328": {
                   "pin_code": "fdfd",
                   "serial_number": "fawwwww"
                }
             }
          }
    }
    }

How do I parse this response since inside data the field name is order id same with voucher

Sonali
  • 447
  • 1
  • 4
  • 19
  • 1
    Does this answer your question? [How to convert json objects with number as field key in Java?](https://stackoverflow.com/questions/17824674/how-to-convert-json-objects-with-number-as-field-key-in-java) – Savior Aug 30 '20 at 18:16
  • Or https://stackoverflow.com/questions/21759625/how-whould-i-parse-json-with-numerical-object-keys-in-jackson-json – Savior Aug 30 '20 at 18:16

1 Answers1

1

If you use Jackson JSON library, you should have POJOs like those shown below and use PropertyNamingStrategy.SnakeCaseStrategy to handle property names in the input JSON:

// top-level container
public class Response {
    private Map<Integer, Order> data;
    // getter/setter
}

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Order {
    private String value; // may be some Currency class
    private Integer orderId;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderDate;

    private String categoryName;
    private Integer brandId;
    private String brandName;
    private String denominationName; // may be Currency too
    private Integer orderQuantity;
    private String vendorOrderId;
    private String vendorLocation;
    private Map<Integer, Voucher> vouchers;

    // getters/setters
}

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Voucher {
    private String pinCode;
    private String serialNumber;
    
    // getters/setters
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42