Convert object values into an array

Khanna Ji
Tera Guru

Hi All,

I have a CS and SI with ajax calls to fetch records from a table. I can see that records are being fetch from SI as a JSON object because I am passing them as JSON object.

When I am printing the answer in the client script as get something like below;

[{"lable":"value1"},{"label":"value2"},{"label":"value3"},{"label":"value4"},{"label":"value5"}.....

Now, I want to extract only values from the above answer in the client script and push it to the array. I need to pass the array to a function. How can I do that?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can parse the JSON like this

Script:

var str = '[{"label":"value1"},{"label":"value2"},{"label":"value3"},{"label":"value4"},{"label":"value5"}]';

var parser = JSON.parse(str);

var arr = [];

for(var i=0;i<parser.length;i++){

var val = parser[i].label;
arr.push(val.toString());

}

gs.info('my values->' + arr.toString());

Output:

find_real_file.png

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can parse the JSON like this

Script:

var str = '[{"label":"value1"},{"label":"value2"},{"label":"value3"},{"label":"value4"},{"label":"value5"}]';

var parser = JSON.parse(str);

var arr = [];

for(var i=0;i<parser.length;i++){

var val = parser[i].label;
arr.push(val.toString());

}

gs.info('my values->' + arr.toString());

Output:

find_real_file.png

Regards
Ankur

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

@Khanna Ji 

Glad to know that my script worked.

Please mark response helpful as well.

Regards
Ankur

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

Hi Ankoor,

I have almost similar issue. After converting the object into array it is not working correctly. When I am passing only one country it is working but when I am passing more than one country it is not working as shown below. Can you tell me why is it happening?

1. When I am passing only one country in "inputs"

     a. First log shows as

         4d38b7111b121100763d91eebc0713f1

     b. Second log shows as

                               u_role=7491fa08db0dac14842b7b75f39619a9^u_countryIN4d38b7111b121100763d91eebc0713f1 (DEBUG)

2. When I am passing more than one country in "inputs"

     a. First log shows as

         4d38b7111b121100763d91eebc0713f1,c138b7111b121100763d91eebc0713f2

     b. Second log shows as

          u_role=7491fa08db0dac14842b7b75f39619a9^u_countryIN (DEBUG)

 

Code:

_getApprovers: function(inputs, outputs) {
_this = this;

this.ticketReference = inputs.ticket_reference;
var roleSysID = inputs.role.toString();
var countryListObj = inputs.countries;
var countryList = [];
countryList.push(countryListObj.toString());
gs.log(countryList, "Test1");

var grRoleApprovals = new GlideRecord("u_hr_role_approvals");
grRoleApprovals.addQuery("u_role", roleSysID);
grRoleApprovals.addQuery("u_country", "IN", countryList.toString());
grRoleApprovals.query();

_this._log("Query used to find Approval Definitions for current Business Unit: " + grRoleApprovals.getEncodedQuery(), "DEBUG");

while (grRoleApprovals.next()) {
.
.
.
.
.
}