-2

I have request body of supplier in below format :

{
  "supplier": {
    "supplierData": {
      "supplierAddress": {
        "street": "abc",
        "postCode": "1234",
        "city1": "abcd",
        "country": "DE",
        "region": "BW"
      },
      "location": {
        "locationID": "1234",
        "locationName": "South Africa "
      },
      "lastUpdatedDateTime": "2022-06-28T10:07:37.000Z"
    }    
  }
}

able to consume it and need to know how to consume the below body request for Location object as null instead of string :

{
  "supplier": {
    "supplierData": {
      "supplierAddress": {
        "street": "abc",
        "postCode": "1234",
        "city1": "abcd",
        "country": "DE",
        "region": "BW"
      },
      "location": "null",
      "lastUpdatedDateTime": "2022-06-28T10:07:37.000Z"
    }    
  }
}

Any idea how we would do it.

nitin
  • 83
  • 1
  • 9

1 Answers1

0

I'm assuming you want to parse this into a defined POJO. And also assuming that Location must be an Object, otherwise (a string), it is null, then you can try parse it:

JSONObject jo = new JSONObject(jsonString); //use your parser library method

Location location = null;

try{
     JSONObject jo2 = jo.getJsonObject(location);
     //parse jo2 into location
}
catch(JSONException e){
     //if exception is throw, it would mean the location data is a string, which can only be "null"
}
Lumethys
  • 81
  • 5