Script help: Json printing only [object object]

Rupal Mehta
Kilo Contributor

Trying below script but the JSON only prints [object object]. What is the mistake here.

if (gr.isValidRecord()) {

result.u_class = gr.u_env_class;

result.u_status = gr.install_status;

}

return JSON.stringify(result);
1 ACCEPTED SOLUTION

Shubham Tipnis
Kilo Sage
Kilo Sage

Hi Rupal,

 

gr.field_name returns the object. It's better to convert it to string while saving it in JSON.

 

if (gr.isValidRecord()) {

 

result.u_class = gr.u_env_class.toString();

 

result.u_status = gr.install_status.toString();

 

}

 

return JSON.stringify(result);

 

Please mark correct/helpful if applicable.

 

Regards,

Shubham

Regards,
Shubham Tipnis
 ServiceNow Enthusiast
️ 3x Rising Star (2022–2024) – ServiceNow Community
 Sharing insights, use cases & real-world learnings from the Now Platform
 Always learning. Always building.

View solution in original post

5 REPLIES 5

Shubham Tipnis
Kilo Sage
Kilo Sage

Hi Rupal,

 

gr.field_name returns the object. It's better to convert it to string while saving it in JSON.

 

if (gr.isValidRecord()) {

 

result.u_class = gr.u_env_class.toString();

 

result.u_status = gr.install_status.toString();

 

}

 

return JSON.stringify(result);

 

Please mark correct/helpful if applicable.

 

Regards,

Shubham

Regards,
Shubham Tipnis
 ServiceNow Enthusiast
️ 3x Rising Star (2022–2024) – ServiceNow Community
 Sharing insights, use cases & real-world learnings from the Now Platform
 Always learning. Always building.

In my case - I had different scenario but the way you suggested to convert to string really worked well as my result printing as empty object {} though it has right value in it because it was in [Object Object] format and now without using for each loop I can get the object value and can print the JSON in format. I searched so many articles and found this perfect solution.

 

Thanks for your suggestion!

Abhay Kumar1
Giga Sage
Hi Rupal, please try gr.getDisplayValue('u_env_class')/gr.getValue('u_env_class') instead of gr.ur_env_class ,change both place and then check. Hope this will help.

Anurag15
Giga Expert

Hi,

Please convert that JSON object to string using - 

 

JSON.stringify(json_object_name);