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
  • 1.  Convert Json Fields to camelCase

    Posted Wed February 17, 2021 03:02 PM

    Dears,

    How can i achieve the below scenario:

    ActualResponse:

    {

    "Msg_Format": "1234"

    }

    Expected Response:

    {

    "msgFormat": "1234"

    }

    how to dothis using gatewayscript



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: Convert Json Fields to camelCase

    Posted Thu February 18, 2021 10:29 AM

    Hi San,

    you can try the following:

    //Set the required libraries

    var hm = require('header-metadata');

    //Function for traversing through JSON tree

    function convertKeys(obj) {

    // Temporary object for storing traverse results

    var tmpObj = {};

    // Process array objects

    if (Array.isArray(obj)) {

    var arr = [];

    // traverse through array content

    for ( var i in obj) {

    if (obj[i] !== null && typeof (obj[i]) == "object") {

    arr.push(convertKeys(obj[i]));

    } else {

    arr.push(obj[i]);

    }

    }

    tmpObj = arr;

    }

    // If not array, process other objects

    else {

    for ( var i in obj) {

    var j;

    if (i.includes("_")) {

    var firstPart = i.split('_')[0].toLowerCase();

    var secondPart = i.split('_')[1];

    j = firstPart.concat(secondPart);

    } else {

    j = i;

    }

    if (obj[i] !== null && typeof (obj[i]) == "object") {

    tmpObj[j] = convertKeys(obj[i]);

    } else {

    // If not array or object, assume that we have string, etc.

    // value

    tmpObj[j] = obj[i];

    }

    }

    }

    return tmpObj;

    }

    // Parse incoming JSON payload

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

    if (error) {

    throw error;

    } else {

    // Use convert keys function call

    var json = convertKeys(reqBody);

    // Log entry for debug purposes

    //console.log(JSON.stringify(json));

    // Set content type to JSON

    hm.current.set("Content-Type", "application/json");

    // Send JSON request body to output

    session.output.write(json);

    }

    });

    --HP



    #DataPower
    #Support
    #SupportMigration


  • 3.  RE: Convert Json Fields to camelCase

    Posted Fri February 19, 2021 12:57 AM

    Dear Hermann,

    Thanks for the reply, for the first json it worked , but for below it is not working:

    Actual:

    {

    "Sam_Dank_Header": "sada",

    "Msg_Version": "0001",

    "BSI_Return_Code" :"",

    "Redit_Ward_Number_Ma" : "",

    "FBValidationVars" : ""

    }

    Expected:

    {

    "samDankHeader": "sada",

    "msgVersion": "0001",

    "bsiReturnCode" :"",

    "reditWardNumberMa" : "",

    "fbValidationVars" : ""

    }

    I want to convert it to pure camelCase



    #DataPower
    #Support
    #SupportMigration


  • 4.  RE: Convert Json Fields to camelCase

    Posted Fri February 19, 2021 12:45 PM

    Try this. Feel free to modify the script for your use case.


    //Set the required libraries

    var hm = require('header-metadata');


    //Function for traversing through JSON tree

    function convertKeys(obj) {

    // Temporary object for storing traverse results

    var tmpObj = {};


    // Process array objects

    if (Array.isArray(obj)) {

    var arr = [];


    // traverse through array content

    for ( var i in obj) {

    if (obj[i] !== null && typeof (obj[i]) == "object") {

    arr.push(convertKeys(obj[i]));

    } else {

    arr.push(obj[i]);

    }

    }

    tmpObj = arr;


    }

    // If not array, process other objects

    else {

    for ( var i in obj) {

    var j = camelize(i);

    if (obj[i] !== null && typeof (obj[i]) == "object") {

    tmpObj[j] = convertKeys(obj[i]);

    } else {

    // If not array or object, assume that we have string, etc.

    // value

    tmpObj[j] = obj[i];

    }

    }

    }

    return tmpObj;

    }


    //Change to CamelCase

    function camelize(str) {

     return str.replace(/_/g, ' ').replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {

      return index === 0 ? word.toLowerCase() : word.toUpperCase();

     }).replace(/\s+/g, '');

    }


    // Parse incoming JSON payload

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

    if (error) {

    throw error;

    } else {


    // Use convert keys function call

    var json = convertKeys(reqBody);


    // Log entry for debug purposes

    //console.log(JSON.stringify(json));


    // Set content type to JSON

    hm.current.set("Content-Type", "application/json");


    // Send JSON request body to output

    session.output.write(json);

    }

    });




    #DataPower
    #Support
    #SupportMigration