Extract specific value from a string field

Vamshi_ch123
Tera Contributor

Hi All, 

In the description field, I have a key value called a job, so I want to extract this job value from the description field and add it in a summary. when I try this using below script it is returning -1 value as input . 

 
var summary =  current.description.valueString[0].indexOf("job:");
 
Result: -1
Description:
"valueString":"[ var='B0' metric='long_running_sessions_value{instance=\"10.130.\", job=\"db_node1\"}' labels={__name__=long_running_sessions_value, instance=10.130, job=prod_db_node1} value=9 ], [ var='B1' metric='long_running_sessions_value{instance=\"10.130\", job=\"b_node2\"}' labels={__name__=oracledb_long_running_sessions_value, instance=10.130, job=prod_db_node2} value=2 ]"

 

Thank you

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, OOB ‘description’ fields are strings and so valueString[0] does not exist in a standard description field, as a standalone array element or an array element that is part of a name: value pair.

Also the formatting of data within your ‘description’ may be inconsistent as I get errors if I try to instantiate the details provided as a string or an object name: value pair in a background script.

If you need to be able to extract a specific array element, then the JSON() methods should provide a solution - once you have your string format corrected, you should be able to instantiate as an object with JSON parse.

JSON | ServiceNow Developers

Also the string posted above does not contain “job:”

 

Can you clarify your field and data format and how the data is populated into the ‘description’ ?

Ankur Bawiskar
Tera Patron
Tera Patron

@Vamshi_ch123 

can you share some business requirement how that description is getting populated and how does it look?

it looks to be a json

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