How to convert this string to an array in ServiceNow server side script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2023 11:40 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2023 12:18 AM
Hi,
Not really sure about what you want help with in this question, but you can try something like this perhaps.
var text = 'How to convert this string to an array in ServiceNow server side script';
var arrayText = [];
arrayText = text.split(' ');
gs.info('array length: ' + arrayText.length);
for (var i=0; i< arrayText.length; i++){
gs.info('Part ' + i + ' of the array: ' + arrayText[i]);
}
/* Output:
*** Script: array length: 13
*** Script: Part 0 of the array: How
*** Script: Part 1 of the array: to
*** Script: Part 2 of the array: convert
*** Script: Part 3 of the array: this
*** Script: Part 4 of the array: string
*** Script: Part 5 of the array: to
*** Script: Part 6 of the array: an
*** Script: Part 7 of the array: array
*** Script: Part 8 of the array: in
*** Script: Part 9 of the array: ServiceNow
*** Script: Part 10 of the array: server
*** Script: Part 11 of the array: side
*** Script: Part 12 of the array: script
*/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2023 12:20 AM
@Snehal13 though you didn't share any string but here I am sharing a sample code using which you can convert a coma separated string to array in a script.
var fruits = 'apple,oranges,graps,banana';
var fruitsArray = fruits.split(',');
gs.info(fruitsArray);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2023 12:22 AM
Hello @Snehal13 ,
Please refer this : https://www.servicenow.com/community/developer-forum/how-to-convert-string-to-array/m-p/1503837
@Snehal13 , Please mark my answer as "Accept as Solution" and "Helpful." If it works for you.
Thank You.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2023 12:44 AM
To convert a string to an array in ServiceNow server-side script, you can use the split() method. The split() method
You can adjust the separator parameter of the split() method to fit your specific use case...