DataPower

DataPower

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
Expand all | Collapse all

how to extract only common key objects from the json array payload using Gateway script?

  • 1.  how to extract only common key objects from the json array payload using Gateway script?

    Posted 11/06/20 01:29 PM

    Hi Team

    I am having the json payload as below. I have to extract the common key objects from the below payload ("empId") using gateway script

    Below is the input and expected output payload. Could you please help me in this?

    Input Json Payload

    {

    "shifts": [

    {

    "startTime": "1530",

    "Test": [

    {

    "Members": [

    {

    "Id": "2339",

    "empId": "12345",

    "firstName": " Glenn",

    "lastName": "Kunsel"

    },

    {

    "Id": "2339",

    "empId": "12345",

    "firstName": " GleT",

    "lastName": "Kunse"

    }

    ],

    "Leader": {

    "Id": "2339",

    "empId": "12345",

    "firstName": " GleT",

    "lastName": "Kunse",

    "primary": {

    "name": "37' Bucket"

    }

    }

    },

    {

    "Members": [

    {

    "Id": "2339",

    "empId": "12345",

    "firstName": " Glenn",

    "lastName": "Kunsel"

    },

    {

    "Id": "2339",

    "empId": "12345",

    "firstName": " GleT",

    "lastName": "Kunse"

    },

    {

    "Id": "2339",

    "empId": "12345",

    "firstName": " GleT",

    "lastName": "Kunse"

    }

    ],

    "Leader": {

    "Id": "2339",

    "empId": "12345",

    "firstName": " GleT",

    "lastName": "Kunse",

    "primary": {

    "name": "37' Bucket"

    }

    }

    }

    ]

    }

    ]

    }

    Expected output

    {

    "shifts": [

    {

    "empId": "12345",

    "empId": "12345",

    "empId": "12345",

    "empId": "12345"

    }

    ]

    }

    thank you

    SK



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: how to extract only common key objects from the json array payload using Gateway script?
    Best Answer

    Posted 11/13/20 07:29 AM

    Hi,

    please see the code below and the resulted output. Feel free to modify it to match your use case. As the problem is basically Javascript related, it might be more useful to post these types of questions to some Javascript forum.

    Gatewayscript:

    // Function for extracting empIds

    function extractIds(obj) {

    var arr = [];

    //Loop through JSON arrays

    for ( var i in obj.shifts) {

    for ( var j in obj.shifts[i].Test) {

    var leader = new Object();

    leader.empId = obj.shifts[i].Test[j].Leader.empId;

    arr.push(leader);

    for ( var k in obj.shifts[i].Test[j].Members) {

    var member = new Object();

    member.empId = obj.shifts[i].Test[j].Members[k].empId;

    arr.push(member);

    }

    }

    }

    //Collect all empIds that have duplicates

    const lookup = arr.reduce((a, e) => {

    a[e.empId] = ++a[e.empId] || 0;

    return a;

    }, {});

    return arr.filter(e => lookup[e.empId]);

    }

    //Read input as JSON

    session.input.readAsJSON(function(error, input) {

    if (error) {

    console.error('readAsJSON error: ' + error);

    } else {

    // Collect all empIds into an array and remove unique values

    var duplicateIds = extractIds(input);

    //Create a new object for the results

    var json = new Object();

    json.shifts = duplicateIds;

    // Write json into output

    session.output.write(json);

    }

    })

    Resulted output:

    {"shifts": [

    {"empId": "12345"},

    {"empId": "12345"},

    {"empId": "12345"},

    {"empId": "12345"},

    {"empId": "12345"},

    {"empId": "12345"},

    {"empId": "12345"}

    ]}

    --HP



    #DataPower
    #Support
    #SupportMigration