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

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****/

dheeraaj
Giga Expert

I think I understood where I was going wrong. 

 

Previously I am using Stringify to convert the object to JSON. 

But I got the Item sysid using the below code: 

 

gs.info('success is:'+input.test.userFav[0].item_sys_id);

 

output : success is:7b2f4535db936b80fd0a38ff9d9619c1

 

For every one who helped me , thank  you very much. 

Here , I am trying to pass the value from client controller to Server Script: 

 

Client Controller : 

 

$scope.goTo = function(item) {

$scope.data.test = item;

$scope.server.update().then(function(){
console.log($scope.data.test);
});
}

 

Server Script  :

if(input.test){

gs.info('success is:'+input.test.userFav[0].item_sys_id);
var l0 = new GlideRecord('sp_log');
l0.initialize();
l0.user = gs.getUserName();
l0.type = 'Portal Favorites';
l0.table = 'u_portal_favorites';
l0.id = jinput.test.userFav[0].item_sys_id;
l0.count = '0';
l0.update();
}

 

Thanks again everyone !

Hi @dheeraaj 

Kindly mark the response as helpful or correct to close the community post.

Thanks,

Vivek Anand