How to remove slash from array of string?

vishalj
Tera Expert

Scenario: I am designing a Action in Flow designer where through scripting i need to pass some array of string value.

Issue: When i run the code in Background Script it gives proper output but when i use same code in Actions it doesn't.

Expected/Correct output format which i am getting in Background script: 

 

"Key":"abc","Key":"def","Key":"ghi","Key":"jkl"

 

 Wrong output format which i am getting in Action by using same script: 

 

"\"Key\":\"01\"","\"Key\":\"02\""

 

 Script used: 

 

var arrStr =["abc","def","ghi","jkl"];
var i;
var arrObj = {"Key1":[]};
for(i=0; i<arrStr.length; i++)
{
var testRec ="\""+"Key"+"\":"+"\""+ arrStr[i]+"\"";
arrObj.Key1.push(testRec);
gs.log(testRec);
}
for (var key in arrObj)
gs.print("Key is: " + key + " and value is: " + arrObj[key]);

 

 

 

Can anyone help in this?

4 REPLIES 4

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @vishalj ,

 

Where exactly you are printing that output from action ?

I tried in my PDI with the same code and it gives me the expected output only. Try to elaborate more so that I can try that on my PDI as well.


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Bert_c1
Kilo Patron

Hi @vishalj,

 

Try replacing the line where you have slashes to:

 

 

var testRec ='"'+'MATDOC_ITEM'+'":'+'"'+ arrStr[i]+'"';

 

I get the same output in scripts background. Use single quotes for string values when you want double quotes in the value.

Hi @Bert_c1 ,

 

I had already tried this but still not working.

Hi @vishalj,

 

Then try:

 

var testRec = "MATDOC_ITEM:"+ arrStr[i];

since both are strings, why enclose in quotes?