Remove double quotes from array values returned

Bhaskar24
Tera Expert

I am getting the response in Client Script from the Script Include as below.

["5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f"]

 

I would like to convert it as below by removing the double quotes.

[5390f47f97e892545a4b31511153af96,2a21e73697709e901b85f0511153afce,42c87060471e46d0fcf52501e36d436f]

 

Please help me to achieve this by adding the piece of code in Script Include or Client Script.

1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @Bhaskar24 

 

If you just want to get rid of the double quotes, you can convert your array to string followed by replacing all the double quotes and finally trimming the white spaces. Refer below screenshots and code:

 

AmitVerma_0-1736822351685.png

 

var arr = ["5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f"];
arr = arr.toString().replaceAll('"',"").trim();
gs.print(arr);

Output -

AmitVerma_1-1736822387811.png

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

9 REPLIES 9

Nick Parsons
Mega Sage

A list collector's value should be a string in the format of sys_id1,sys_id2,sys_id3 so without the brackets. What you want is to join your array of strings into one string, which you can do with .join():

 

var objAnswer = JSON.parse(answer);
var arrAddGroups = objAnswer.addGroups; // ["sys_id1", "sys_id2", ...]
var strAddGroups = arrAddGroups.join(','); // "sys_id1,sys_id2,..."

g_form.setValue("add_groups", strAddGroups);

 

Thank you for the response Nick. Sorry, I tried it but no luck.

Client Script updated as below.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    var userID = g_form.getValue('requested_for');
    var holdingGroups = g_form.getValue('holding_memberships');
    g_form.showFieldMsg('add_remove_groups', 'Old Groups: ' + oldValue);
    g_form.showFieldMsg('add_remove_groups', 'New Groups: ' + newValue);
    g_form.showFieldMsg('add_remove_groups', 'Holding Groups: ' + holdingGroups);

    var ga = new GlideAjax('ManageAtYourServiceMembers');
    ga.addParam('sysparm_name', 'validateMembership');
    ga.addParam('sysparm_requested_for', userID);
    ga.addParam('sysparm_holdingGroups', holdingGroups);
    ga.addParam('sysparm_updatedGroups', newValue);
    ga.getXMLAnswer(function(answer) {
        var objAnswer = JSON.parse(answer);
        var arrAddGroups = objAnswer.addGroups;        
        var strAddGroups = arrAddGroups.join(',');

        g_form.showFieldMsg('add_groups', 'Add Groups Obj: ' + strAddGroups);
        g_form.showFieldMsg('remove_groups', 'Remove Groups Obj: ' + objAnswer.removeGroups);

    });
}

 

Response received as below.

Add Groups Obj: "dfb2f909dbe76200b81ad8c5ce96194f","11d14762db89b704a949e59b8a961958"

Thank you for the response Nick. Sorry, I tried it but no luck.

 

Client Script updated as below.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    var userID = g_form.getValue('requested_for');
    var holdingGroups = g_form.getValue('holding_memberships');
    g_form.showFieldMsg('add_remove_groups', 'Old Groups: ' + oldValue);
    g_form.showFieldMsg('add_remove_groups', 'New Groups: ' + newValue);
    g_form.showFieldMsg('add_remove_groups', 'Holding Groups: ' + holdingGroups);

    var ga = new GlideAjax('ManageAtYourServiceMembers');
    ga.addParam('sysparm_name', 'validateMembership');
    ga.addParam('sysparm_requested_for', userID);
    ga.addParam('sysparm_holdingGroups', holdingGroups);
    ga.addParam('sysparm_updatedGroups', newValue);
    ga.getXMLAnswer(function(answer) {
        var objAnswer = JSON.parse(answer);
        var arrAddGroups = objAnswer.addGroups;        
        var strAddGroups = arrAddGroups.join(',');

        g_form.showFieldMsg('add_groups', 'Add Groups Obj: ' + strAddGroups);
        g_form.showFieldMsg('remove_groups', 'Remove Groups Obj: ' + objAnswer.removeGroups);

    });
}

 

Response received as below.

Add Groups Obj: "dfb2f909dbe76200b81ad8c5ce96194f","11d14762db89b704a949e59b8a961958"

Amit Verma
Kilo Patron
Kilo Patron

Hi @Bhaskar24 

 

If you just want to get rid of the double quotes, you can convert your array to string followed by replacing all the double quotes and finally trimming the white spaces. Refer below screenshots and code:

 

AmitVerma_0-1736822351685.png

 

var arr = ["5390f47f97e892545a4b31511153af96","2a21e73697709e901b85f0511153afce","42c87060471e46d0fcf52501e36d436f"];
arr = arr.toString().replaceAll('"',"").trim();
gs.print(arr);

Output -

AmitVerma_1-1736822387811.png

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Thank you very much for your help, Amit! It worked.