Here this is my JSON which I have as a string "additionalThirdParty"
{
"header": null,
"object": {
"ASSETS": {
"productDetails": [{
"productId": "PT_230",
"productThirdPartyDetails": [{
"thirdPartyId": "TH12",
"Location": "France",
"addtionalInfo": []
}]
}]
}
}
}
Now my requirement is to add an extra JSON object inside "productThirdPartyDetails" array.
The new JSON object will be
{
"thirdPartyId": "TH11",
"Location": "Belgium",
"addtionalInfo": []
}
So the final object should be like
{
"header": null,
"object": {
"ASSETS": {
"productDetails": [{
"productId": "PT_230",
"productThirdPartyDetails": [{
"thirdPartyId": "TH12",
"Location": "France",
"addtionalInfo": []
},
{
"thirdPartyId": "TH11",
"Location": "Belgium",
"addtionalInfo": []
}
]
}]
}
}
}
I'm trying to add as follows.
converting this string to JSON as
JSONObject obj = new JSONObject(additionalThirdParty);
Iterator<?> keys = obj.keys();
while (keys.hasNext()) {
JSONObject assetsObj = obj.getJSONObject("ASSETS");
Iterator<?> assetKeys = assetsObj.keys();
while (assetKeys.hasNext()) {
JSONArray productDetails = assetsObj.getJSONArray("productDetails");
logger.info("productDetails=" + productDetails);
}
}
It throws me that org.json.JSONException: JSONObject["ASSETS"] not found.
Any ideas on how to add a new object into that array
productDetailsarray have only one element? or you want to add the new element for the productId"productId": "PT_230"