Original Message:
Sent: Tue June 06, 2023 06:55 AM
From: Ashok Beshra
Subject: Iterating through JSON array in API Connect Gatewayscript
Hi Steve...
We want to iterate through the element in the array within the map policy. There is requirement where we need to encrypt certain fields like 'AccBal' in the above response. I am not able to access the element so that same can be used for encryption in the map policy.
I am able to access individual element without array by referencing them as $(fieldpath) and then encrypt. Can you suggest what would be the right syntax to access the element in map policy of gateway script for array element. Below is the one used by us.
var retValue = undefined;
console.error("AccountBal :",$(input.GetAccountListRes.GetAccountList.item(0).AccBal))
encryption logic
retValue = encryptedAccountBal
------------------------------
Ashok Beshra
Original Message:
Sent: Sun April 02, 2023 03:06 PM
From: Ashok Beshra
Subject: Iterating through JSON array in API Connect Gatewayscript
Thanks a lot Steve. The above code works perfectly.
------------------------------
Ashok Beshra
Original Message:
Sent: Fri March 31, 2023 08:32 AM
From: Steve Linn
Subject: Iterating through JSON array in API Connect Gatewayscript
Hi Ashok,
Your XML has repeating CustomGetAccountList elements. Your current code is iterating over those elements but is always setting each entry into the same GetAccountList output object and thus you see only the last entry. What you should be doing is specifying GetAccountList as an empty array to start.
resp[respRoot].GetAccountList = [];
Then as you process the current element, set some temporary variable as an empty object and populate it and then push that temp object to the GetAccountList array, something like ...
let tempObj = {};
var AccNoElement = acctDtlResponse.getElementsByTagNameNS("http://www.infosys.com/response/CustomGetAccountList", "ACC_NUM").item(0);tempObj.AccNum = AccNoElement.textContent;console.error('AccNum ' + AccNoElement.textContent);var AccCurrElement = acctDtlResponse.getElementsByTagNameNS("http://www.infosys.com/response/CustomGetAccountList", "ACC_CURR").item(0);tempObj.AccCur = AccCurrElement.textContent;var AccNameElement = acctDtlResponse.getElementsByTagNameNS("http://www.infosys.com/response/CustomGetAccountList", "ACC_NAME").item(0);tempObj.AccName = AccNameElement.textContent;var AccBalElement = acctDtlResponse.getElementsByTagNameNS("http://www.infosys.com/response/CustomGetAccountList", "ACC_BAL").item(0);tempObj.AccBal = AccBalElement.textContent;
resp[respRoot].GetAccountList.push(tempObj);
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Thu March 30, 2023 09:48 AM
From: Ashok Beshra
Subject: Iterating through JSON array in API Connect Gatewayscript
Hi Team...
We are trying to map the backend SOAP response message and map to an outgoing JSON response message in the Gateway script. We are facing issue while mapping the multi-valued complex types to JSON array where only one instance of the array is getting mapped in the response. Please find the attached gatewayscript and help us to map all instances of JSON array in the response. Thanks
Expected Response
{
"GetAccountListRes": {
"GetAccountList": [{
"AccNum": "XXXXXXXXXXXXXXXXXX",
"AccCur": "INR",
"AccName": "XXXXXXXXXXXXXXXXXX",
"AccBal": "212134.55"
}, {
"AccNum": "XXXXXXXXXXXXXXXXXX",
"AccCur": "USD",
"AccName": "XXXXXXXXXXXXXXXXXX",
"AccBal": "532134.55"
}, {
"AccNum": "XXXXXXXXXXXXXXXXXX",
"AccCur": "AED",
"AccName": "XXXXXXXXXXXXXXXXXX",
"AccBal": "112134.55"
}]
}
}
Current Response
{
"GetAccountListRes": {
"GetAccountList": {
"AccNum": "XXXXXXXXXXXXXXXXXX",
"AccCur": "AED",
"AccName": "XXXXXXXXXXXXXXXXXX",
"AccBal": "112134.55"
}
}
}
------------------------------
Ashok Beshra
------------------------------