0
{
    "DistributionOrderId" : "Dist_id_1",
    "oLPN": 
    {
        "Allocation": 
        {
            "AID": "12345"      
        },
        "Allocation": 
        {
            "AID": "123456" ,
            "SerialNbr": "SRL001",
            "BatchNbr": "LOT001"
            "RevisionNbr": "RVNBR1"
        }
    },
    "oLPN": 
    {
        "Allocation": 
        {
            "AID": "12123"      
        }
        "Allocation": 
        {
            "AID": "12124"      
        }
    }
}

I have a JSON request passed from the vendor, How to store the values as Java POJO and use them further? Edit : Added attributes to JSON

2 Answers2

0

json key can't repeat, and the repeat key with value will be discard. i have format your json text, may be it is like following expression:

{
    "DistributionOrderId" : "Dist_id_1",
    "oLPN": 
    [
      {
        "Allocation": 
          [
            {
              "AID": "123456" ,
              "SerialNbr": "SRL001",
              "BatchNbr": "LOT001",
              "RevisionNbr": "RVNBR1"
            },
            {
              "AID": "12345"
            }
          ] 
      },
      {
          "Allocation": 
          [
            {
                "AID": "12123"      
            },
            {
                "AID": "12124"      
            }
          ]
      }
    ]
}

and this struct match the java object is

class DistributionOrder{
  @JsonProperty("DistributionOrderId")
  String distributionOrderId;
  List<OLPN> oLPN;
}

class OLPN {
  @JsonProperty("Allocation")
  List<Allocation> allocation;
}

class Allocation{
  String AID;
  @JsonProperty("SerialNbr")
  String serialNbr;
  @JsonProperty("BatchNbr")
  String batchNbr;
  @JsonProperty("RevisionNbr")
  String revisionNbr;
}
wanglong
  • 89
  • 5
  • In my case I have multiple oLPN objects passed in the Json request, and also each olpn object will have mandatory weight and uom attribute. And each Allocation object will have optional "SerialNbr": "", "BatchNbr": "" “RevisionNbr”: “” attributes. What will be the correct JSON format in this case? – Harshith Rao Mar 25 '21 at 07:05
  • maybe you can send a example text with the most complex, json type can be expand, whatever you need @HarshithRao – wanglong Mar 25 '21 at 07:19
  • what you send means the oLPN and Allocation both array, and i would to say again: and json string can't hold same key name in same level. with what you need i have change my answer, if you also have question, just reply! @HarshithRao – wanglong Mar 25 '21 at 07:52
  • yes, i got it.. serialNbr,batchNbr,revisionNbr will not be passed always inside Allocation array. So in my Allocation POJO class, which json annotation should be used? ``` @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public class Allocation { private String AID; private String SerialNbr; private String BatchNbr; private String RevisionNbr; ``` will above work? – Harshith Rao Mar 25 '21 at 09:37
  • if you want json text to pojo, the pojo must have all field that exists in json text, if you want pojo to json text, @JsonInclude(JsonInclude.Include.NON_NULL) can help filter the null value, just for use, if you understand, choose my answer, if not, just comment follow. – wanglong Mar 25 '21 at 09:44
0

As you aren't using the usual camel case conventions common in jackson databinding I would suggest defining a java object with the appropriate annotations. The object is not currently valid json. If for example the json looks like this:

{
    "DistributionOrderId" : "Dist_id_1",
    "oLPN": [ ... ]
}

Where each oLPN in the array is an object like this:

{
    "Allocation": [{ "AID": ... }, { "AID": ... }, ... ]
}

You can write a class for distribution orders

public class DistributionOrder {
    @JsonProperty("DistributionOrderId") private String id;
    @JsonProperty("oLPN") private List<OLPN> olpn;
    // getters and setters
}

Then another for the OLPN object

public class OLPN {
    @JsonProperty("Allocation") private String allocation;
    @JsonProperty("AID") private String aid;
    // getters and setters
}

Then you can use an object mapper as appropriate. For example

ObjectMapper mapper = ... // get your object mapper from somewhere

DistributionOrder distributionOrder = mapper.readValue(raw, DistributionOrder.class);

See also object mapper javadoc

geco17
  • 5,152
  • 3
  • 21
  • 38
  • I have few attributes under Allocation like "SerialNbr", "BatchNbr", "RevisionNbr", which are optional Field. And also "weight" and "uom" attributes to Olpn object as mandatory attributes. In this case how to refactor the JSON request? – Harshith Rao Mar 25 '21 at 07:11
  • See https://stackoverflow.com/questions/20578846/ignore-missing-properties-during-jackson-json-deserialization-in-java – geco17 Mar 25 '21 at 07:14