- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-17-2021 11:37 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-17-2021 11:46 PM
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:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-17-2021 11:46 PM
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:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2021 12:58 AM
Glad to know that my script worked.
Please mark response helpful as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2022 11:08 PM
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()) {
.
.
.
.
.
}