API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Iterating through JSON array in API Connect Gatewayscript

    Posted Thu March 30, 2023 09:48 AM
      |   view attached

    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
    ------------------------------

    Attachment(s)



  • 2.  RE: Iterating through JSON array in API Connect Gatewayscript

    Posted Fri March 31, 2023 08:33 AM

    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
    ------------------------------



  • 3.  RE: Iterating through JSON array in API Connect Gatewayscript

    Posted Sun April 02, 2023 03:07 PM

    Thanks a lot Steve. The above code works perfectly.



    ------------------------------
    Ashok Beshra
    ------------------------------



  • 4.  RE: Iterating through JSON array in API Connect Gatewayscript

    Posted Tue June 06, 2023 06:55 AM

    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
    ------------------------------



  • 5.  RE: Iterating through JSON array in API Connect Gatewayscript

    Posted Tue June 06, 2023 09:32 AM

    Hi Ashok,

    A discussion on map is different from this threads topic and deserves a thread to itself.  If a short answer here doesn't help please open a new thread.

    The map policy, when in the UI you create a wire between the input and output schemas, the drawing of the wire will create in the source a map action.  There are two types of actions:

    1. set - This will take the entire contents of the input, specified with the from property, to the output, specified with the value of the set property.
    2. create - This is used for arrays.  It will have a from property, specifying the input array or object (which will be assumed to be an array of one object element), and a foreach which is almost always the same as the from property.  The create also has a property called actions, which would provide set actions for the desired array object's individual properties. 

    You should be using a create action where the input array elements are mapped to the output array elements, and for this one element, you'd also have a value property which is the GatewayScript snippet that is evaluated to determine the mapped value.  In the array's child actions, the input to those actions is only the individual array element, so you would not reference your data as $(input.GetAccountListRes.GetAccountList.AccBal) as each individual AccountList array element is the input and you would reference it as $(AccBal).  The final statement is the result of the evaluation and is not a JavaScript statement, but is just the value you want the snippet to evaluate to, in your case apparently encryptedAccountBal.

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------