parse JSON for "Key " and "Value" using IF condition

Kumar38
Kilo Sage

I have a payload that has  around 15 Key and value pairs. If I use below logic , I can fetch key , value as expected. But  that is resulting in too many iterations. Is there a way to use IF condition directly , without for loop. (These fields are choice fields)

 

 

 

var mapLoad= JSON.parse(gs.getProperty('XXXX'));
//Both Fields are Choice fields
if (mapLoad) {
    for (var key in mapLoad) { 
         if (current.FIELD_NAME_1 == key)
		 {
         current.FIELD_NAME_2=  mapLoad[key]
		 }
    }
}

 

 

 

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @Kumar38 ,

Try below script

var mapLoad = JSON.parse(gs.getProperty('XXXX'));
if (mapLoad && current.FIELD_NAME_1) {
    var key = current.FIELD_NAME_1; // Assuming FIELD_NAME_1 contains the key you want to use
    if (mapLoad.hasOwnProperty(key)) {
        current.FIELD_NAME_2 = mapLoad[key];
    }
}

Thanks,

Anand

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar38 

can you share the json and which key you want to fetch from it?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

It can be anything from the payload , its from a BR , if values changes on field 1 (choice list) , it updates field 2 (Choice list ) , based on key value pair.

 

where as key = Field1 choice 1 : Filed 2 choice 1 and so on

 

{
"Field1_Choice1": "Field2_Choice1",
"Field1_Choice2": "Field2_Choice1",
"Field1_Choice3": "Field2_Choice3",
"Field1_Choice4": "Field2_Choice2"

}

Anand Kumar P
Giga Patron
Giga Patron

Hi @Kumar38 ,

Try below script

var mapLoad = JSON.parse(gs.getProperty('XXXX'));
if (mapLoad && current.FIELD_NAME_1) {
    var key = current.FIELD_NAME_1; // Assuming FIELD_NAME_1 contains the key you want to use
    if (mapLoad.hasOwnProperty(key)) {
        current.FIELD_NAME_2 = mapLoad[key];
    }
}

Thanks,

Anand