parse json array to get key value pair

Reshma77
Tera Contributor

Hello All,

I am trying to populate key-value pair from multi row variable set of incident table. With below code I am getting JSON array

var gr = new GlideRecord('incident');
gr.addQuery('number=INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
gs.print(mrvs);
}

*** Script: [ {
  "name" : "7db62c631b655150ded9eca8ab4bcb90",
  "number" : "007810",
  "manager" : "ABC",
  "cost" : "01394040"
} ]

If I try to parse the multi row variable set (var Payload = JSON.parse(mrvs); gs.print(Payload);) I am getting below result

Script: [object Object]

Can somebody help to get only key value pair from the JSON array. I am using this code in template script that needs to be populated on the ticket
The format should look like below

 

Name: ABC

Number: INC0001164
Manager: XYZ

Thanks,

1 ACCEPTED SOLUTION

Prasad Dhumal
Mega Sage
Mega Sage

Hello Reshma,

Using the following code, you can iterate through the JSON array and print the key-value pairs:

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
var Payload = JSON.parse(mrvs);
for (var i = 0; i < Payload.length; i++) {
var obj = Payload[i];
var keys = Object.keys(obj);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
if (key == "name") {
gs.print("Name: " + obj[key]);
}
if (key == "manager") {
gs.print("Manager: " + obj[key]);
}
}
}
}

View solution in original post

5 REPLIES 5

newhand
Mega Sage

@Reshma77 

It looks like that  mrvs is already a json object, you don't need to use JSON.parse(mrvs) to convert it to a json object.

Try to  such as use mrvs[0] .name  to get the name...

Have a try !

 

Please mark my answer as correct and helpful based on Impact.

Prasad Dhumal
Mega Sage
Mega Sage

Hello Reshma,

Using the following code, you can iterate through the JSON array and print the key-value pairs:

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
var Payload = JSON.parse(mrvs);
for (var i = 0; i < Payload.length; i++) {
var obj = Payload[i];
var keys = Object.keys(obj);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
if (key == "name") {
gs.print("Name: " + obj[key]);
}
if (key == "manager") {
gs.print("Manager: " + obj[key]);
}
}
}
}

if (key == "name") {
gs.print("Name: " + obj[key]);
}

 

While using above code I am getting sys id of the field. How can I get the display value?

I have already tried getdisplayvalue() and tostring() methods it is giving me null value

Pavankumar_1
Mega Patron

Hi @Reshma77 ,

can you check below it will help you to get the data and use wherever it is required.

https://www.servicenow.com/community/developer-forum/get-values-from-mrvs/m-p/1864581

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar