How to get the length of a nested json object array?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2021 11:12 PM
o
I have tried looking for solutions for the past 3 hours but nothing seems to work. I'm trying to get the length of the nested json file that I get on a third-party website but when I tried to log them, It doesn't appear.
// The Json file from another website
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = {};
var orderInfoListItem = {};
var progressInfoItem = {
soCenterReport: "",
EntryNo: "14344"
};
var baseInfoItem = {
customerNetworkNumber: "141",
representativeNumber: "143"
};
var formInfoItem = {
DateNo: "12345678999",
salesChannelName: "2"
};
body = {
orderInfoList: {
progressInfo: progressInfoItem,
baseInfo: baseInfoItem,
formInfo: formInfoItem
}
};
response.setContentType('application/json');
response.setBody(body);
})(request, response);
// The code I got
var r = new sn_ws.RESTMessageV2('x_476173_ac_sopf_s.ORDER','TESTING');
r.setStringParameterNoEscape("serviceId", serviceId);
r.setStringParameterNoEscape("orderStatus", status);
var response = r.execute();
var responseBody = response.getBody();
var jsonform = JSON.parse(responseBody);
var httpStatus = response.getStatusCode();
var result = [];
result.push(httpStatus);
result.push(jsonform);
var orderList = result.orderInfoList;
if(orderList.length > 0){
gs.info('The count supposed to apoear here but it doesnt');
}
On the last part, oderList, Im tryign to get the length.
- Labels:
-
Scoped App Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2021 02:25 AM
Hey,
So you are pushing 'jsonform' into your result array.
Is 'orderInfoList' a property of 'jsonform'?
If so - you want to set orderList variable as the following:
var orderList = result.jsonform.orderInfoList;
Try this and see if it works.
Alternatively print out 'jsonform' so you know exactly where to parse out the orderInfoList value.
// after this line
var jsonform = JSON.parse(responseBody);
// add the below line to confirm where orderInfoList property sits
gs.info("jsonform looks like " + JSON.stringify(jsonform));
Let me know how you get on.
Geoff

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2021 03:51 AM
What are trying to achieve with this line:
var orderList = result.orderInfoList;
As I believe this will return you undefined in result variable.
Tell us the actual thing you want to store in result variable?