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

Getting undefined value when retrieving value from a JSON object

dheeraaj
Giga Expert

Hi All,

I have the below JSON object: 

json69 is:{"last_updated":"2019-11-16 04:28:46","catalog_item":{"display_value":"","value":null},"hr_criteria":{"display_value":"US All Employees",
"value":"31df2c2adb61a7003acf30cf9d9619ca"},"icon":"success-icon-wand.png","active":"1","hover_icon":"success-icon-hover-wand.png",
"userFav":[{"sys_id":"e83663c3db814410fd0a38ff9d96199e","url_target":"_blank","icon":"success-icon-wand.png","typeString":"Link","item_sys_id":"7b2f4535db936b80fd0a38ff9d9619c1",
"hover_icon":"success-icon-hover-wand.png","href":"https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844","title":"Wand","table":null,"order":"0"}],
"title":"Wand","url":{"display":"https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
"value":"https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844"},"sys_id":"7b2f4535db936b80fd0a38ff9d9619c1","url_target":"_blank",
"typeString":"Link","checked":true}

 

I want to access the item_sys_id from the above JSON object. However when I am trying to retrieve it , it is giving me undefined.

 

Help is appreciated.

Regards,

Dheeraaj

 

1 ACCEPTED SOLUTION

VIVEK ANAND
Mega Guru

Hi @dheeraaj 

I used exactly same JSON object which you had. Copy paste the below in background script and hit "Run Script"

Refer the gs.print, that is what you are looking for.

Thanks,
Vivek Anand

Please Note: If my response helps you to fix this issue, please mark it as correct!

 

var json69 = 
{
    "last_updated": "2019-11-16 04:28:46",
    "catalog_item": {
        "display_value": "",
        "value": null
    },
    "hr_criteria": {
        "display_value": "US All Employees",
        "value": "31df2c2adb61a7003acf30cf9d9619ca"
    },
    "icon": "success-icon-wand.png",
    "active": "1",
    "hover_icon": "success-icon-hover-wand.png",
    "userFav": [
        {
            "sys_id": "e83663c3db814410fd0a38ff9d96199e",
            "url_target": "_blank",
            "icon": "success-icon-wand.png",
            "typeString": "Link",
            "item_sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
            "hover_icon": "success-icon-hover-wand.png",
            "href": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
            "title": "Wand",
            "table": null,
            "order": "0"
        }
    ],
    "title": "Wand",
    "url": {
        "display": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
        "value": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844"
    },
    "sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
    "url_target": "_blank",
    "typeString": "Link",
    "checked": true
};

gs.print(json69.userFav[0].item_sys_id);

/****OUTPUT****

7b2f4535db936b80fd0a38ff9d9619c1

****OUTPUT****/

View solution in original post

7 REPLIES 7

vkachineni
Kilo Sage

var data = {your JSON};

console.log(data.userFav[0].item_sys_id);
 
will give item_sys_id
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Hi,

 

I have tried the above script but got the below error : 

org.mozilla.javascript.EcmaError: Cannot read property "0" from undefined

 

Regards,

Dheeraj

Naveen20
ServiceNow Employee
ServiceNow Employee

var myObj, x;
myObj = {
"last_updated": "2019-11-16 04:28:46",
"catalog_item": {
"display_value": "",
"value": null
},
"hr_criteria": {
"display_value": "US All Employees",
"value": "31df2c2adb61a7003acf30cf9d9619ca"
},
"icon": "success-icon-wand.png",
"active": "1",
"hover_icon": "success-icon-hover-wand.png",
"userFav": [
{
"sys_id": "e83663c3db814410fd0a38ff9d96199e",
"url_target": "_blank",
"icon": "success-icon-wand.png",
"typeString": "Link",
"item_sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
"hover_icon": "success-icon-hover-wand.png",
"href": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
"title": "Wand",
"table": null,
"order": "0"
}
],
"title": "Wand",
"url": {
"display": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
"value": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844"
},
"sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
"url_target": "_blank",
"typeString": "Link",
"checked": true
}

 


x = myObj["userFav"][0].item_sys_id;

Prateek kumar
Mega Sage

Try below:


var payload =
{
   
   "userFav": [
      {
         "sys_id": "e83663c3db814410fd0a38ff9d96199e",
         "url_target": "_blank",
         "icon": "success-icon-wand.png",
         "typeString": "Link",
         "item_sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
         "hover_icon": "success-icon-hover-wand.png",
         "href": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
         "title": "Wand",
         "table": null,
         "order": "0"
      }
   ],
   "title": "Wand",
   "url": {
      "display": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844",
      "value": "https://myapps.microsoft.com/success.com/signin/WAND/70621e21-d8e5-47d0-ba36-3e9eb4660844"
   },
   "sys_id": "7b2f4535db936b80fd0a38ff9d9619c1",
   "url_target": "_blank",
   "typeString": "Link",
   "checked": true
};


var x= payload.userFav;
var myJSON = JSON.stringify(x);
gs.print(myJSON);
var y='';
for(i in payload.userFav){
y+=payload.userFav[i].item_sys_id;
}
gs.print(y);

Please mark my response as correct and helpful if it helped solved your question.
-Thanks