The CreatorCon Call for Content is officially open! Get started here.

Searching JSON Field for Key-Value Pairs

Dimension_1111
ServiceNow Employee
ServiceNow Employee

Hi Team,

I have a table with a JSON field, and I need to search for a specific key-value pair within that JSON data. For example, given the JSON data:

{"sys_id": "1", "name": "Hello world", "class": "Business application"}, {"sys_id": "2", "name": "Service Now", "class": "world"} I want to perform a query to find entries where the "name" field contains the value "world." How can I achieve this?

I want to search in the "Json data" field in the below attached screenshot

3 REPLIES 3

Eshwar Reddy
Kilo Sage

Hi @Dimension_1111 


if it is a array of object 
 var sample  = [ {"sys_id": "1", "name": "Hello world", "class": "Business application"}, {"sys_id": "2", "name": "Service Now", "class": "world"} ]


var jsonData = JSON.parse(sample);

 

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

{

var item = jsonData[i];

 

if (item.name && item.name.includes("world"))

{

gs.info("Match found: " + JSON.stringify(item));

}

 

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution

No if you check the screenshot attached Json data is the field in the "sn_grc_indicator_supporting_data" table. I want to search on that field.

var a = {
'sysid': 1, 'name': 'esh', 'age': 27
};


if(a.hasOwnProperty('name'))
{
if(a.name.includes('esh'))
{
gs.info('true');
}
}
Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution