Using script to transform a field value output in flow designer

ShriyaR
Tera Contributor

I have written this code to transform additional info but the line var key in additional info is printing nothing when I'm trying to see what's coming in key. 

 

function transformPayload(additionalInfo) {

    const formattedPayload = {};

 

    for (var key in additionalInfo) {

        var newKey = key.replace(/flattened\.alarmMetaData\.0\./g, '');

        formattedPayload[newKey] = additionalInfo[key];

    }

 

    return formattedPayload;

}

 

const severity = mapObj(fd_data.look_up_record.record.severity);

const alertNum = fd_data.look_up_record.record.number;

const ciorNode = fd_data.look_up_record.record.node;

const alertType = fd_data.look_up_record.record.type.name;

const alertSource = fd_data.look_up_record.record.source;

const additionalInfo = fd_data.look_up_record.record.additional_info;

 

const parsedAdditionalInfo = JSON.parse(additionalInfo);

const rawMetricName = parsedAdditionalInfo["flattened.alarmMetaData.0.query"];

const regex = /^[a-zA-Z]*\b/;

const metricName = regex.exec(rawMetricName)[0];

const alarmSummary = parsedAdditionalInfo["flattened.alarmMetaData.0.alarmSummary"];

const region = parsedAdditionalInfo["flattened.alarmMetaData.0.dimensions.0.region"];

const resourceId = parsedAdditionalInfo["resourceId"];

const resourceType = parsedAdditionalInfo["resourceType"];

 

const responseBlock = transformPayload(parsedAdditionalInfo);

 

return actualResponse;

 

 

Additional info looks like below

 

{

 

"flattened.alarmMetaData.0.dimensions.0.deploymentId":

 

"Xyz",

 

"flattened.alarmMetaData.0.dimensions.0.deploymentName": "source",

 

"flattened.alarmMetaData.0.dimensions.1.deploymentId": "abc",

 

"flattened.alarmMetaData.0.dimensions.1.deploymentName": "target",

 

"flattened.alarmMetaData.0.dimensions.2.deploymentId":

 

"Fgh",

......

}

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, based on your supplied 'payload' you are creating strings within an object to represent the payload hierarchy, but as you are including '.' in the key names you are then unable to dot.walk the results.
Is there a reason why you are trying to flatten your payload keys like this, IE do you need the name path as part of your result? If this is a valid requirement then perhaps your solution is to stringify the payload and use javascript substring() and indexOf() to parse out your names and values?

If not, then you should consider using standard dot.walk functionality for access values in your JSON object without any attempt to 'flatten' the format.
Unfortunately, with no clear details of your source payload the community can only guess at the required solution but based on details supplied I guess at something like this.

var obj = '{"alarmMetaData": {"dimensions": [{"deploymentName": "source","deploymentId": "Xyz"},{"deploymentName": "target","deploymentId": "abc"}]}}';

var test = global.JSON.parse(obj);

gs.info(test.alarmMetaData.dimensions[0].deploymentName);
gs.info(test.alarmMetaData.dimensions[0].deploymentId);
gs.info(test.alarmMetaData.dimensions[1].deploymentName);
gs.info(test.alarmMetaData.dimensions[1].deploymentId);