Undefined JSON Parsed Variable

Ben Cervantes
Kilo Expert

Hello Everyone, 

This is my first API Integration and I'm having a some issues with the JSON Parse to retrieve the properties of the JSON string. When I try to create a new variable from the string and print it, the variable is coming back as undefined. Below is the JSON string I am trying to parse and the script I am using. I am testing this with a background script but will be moving this to a scheduled job. 

{
   "AssetWarrantyResponse": [
      {
         "AssetHeaderData": {},
         "ProductHeaderData": {},
         "AssetEntitlementData": [
            {
               "StartDate": "2015-10-25T00:00:00",
               "EndDate": "2017-10-24T23:59:59",
               "ServiceLevelDescription": "Onsite Service After Remote Diagnosis (Consumer Customer)/ Next Business Day Onsite After Remote Diagnosis (Commercial Customer)",
               "ServiceLevelCode": "ND",
               "ServiceLevelGroup": 5,
               "EntitlementType": "EXTENDED",
               "ServiceProvider": "UNY",
               "ItemNumber": "995-8004"
            },
            {
               "StartDate": "2014-10-24T00:00:00",
               "EndDate": "2015-10-24T23:59:59",
               "ServiceLevelDescription": "Onsite Service After Remote Diagnosis (Consumer Customer)/ Next Business Day Onsite After Remote Diagnosis (Commercial Customer)",
               "ServiceLevelCode": "ND",
               "ServiceLevelGroup": 5,
               "EntitlementType": "INITIAL",
               "ServiceProvider": "UNY",
               "ItemNumber": "995-7974"
            },
            {
               "StartDate": "2014-10-24T00:00:00",
               "EndDate": "2022-10-27T23:59:59",
               "ServiceLevelDescription": "Dell Digitial Delivery",
               "ServiceLevelCode": "D",
               "ServiceLevelGroup": 11,
               "EntitlementType": "INITIAL",
               "ServiceProvider": "DELL",
               "ItemNumber": "421-9983"
            },
            {
               "StartDate": "2014-10-24T00:00:00",
               "EndDate": "2022-10-27T23:59:59",
               "ServiceLevelDescription": "Dell Digitial Delivery",
               "ServiceLevelCode": "D",
               "ServiceLevelGroup": 11,
               "EntitlementType": "INITIAL",
               "ServiceProvider": "DELL",
               "ItemNumber": "422-0007"
            },
            {
               "StartDate": "2014-10-24T00:00:00",
               "EndDate": "2022-10-27T23:59:59",
               "ServiceLevelDescription": "Dell Digitial Delivery",
               "ServiceLevelCode": "D",
               "ServiceLevelGroup": 11,
               "EntitlementType": "INITIAL",
               "ServiceProvider": "DELL",
               "ItemNumber": "421-9982"
            }
         ]
      }
   ],
   "InvalidFormatAssets": {},
   "InvalidBILAssets": {},
   "ExcessTags": {},
   "AdditionalInformation": null
}

var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var expirationDate = parsed.GetAssetWarrantyResponse.AssetEntitlementData.EndDate;
gs.print(expirationDate);

I should be receiving the value of
"2017-10-24T23:59:59" but I am receiving undefined instead. 
1 ACCEPTED SOLUTION

My Mistake, AssetWarrantyResponse is also array of object.

Try like this - 

var parser = new JSONParser();
var parsed = parser.parse(responseBody);

var expirationDate = parsed.AssetWarrantyResponse[0].AssetEntitlementData[0].EndDate;

gs.print(expirationDate);

 

View solution in original post

5 REPLIES 5

Harsh Vardhan
Giga Patron

have you tried to decode the json?

 

kindly refer the thread below.

 

https://community.servicenow.com/community?id=community_question&sys_id=cef647e5db1cdbc01dcaf3231f96...

nayanawadhiya1
Kilo Sage

Hello Ben,

AssetEntitementData is array of object and there is nothing like GetAssetWarrantyResponse on the body.

Try like this -

var obj = JSON.parse(responseBody);

var expirationDate = obj.AssetWarrantyResponse.AssetEntitlementData[0].EndDate;

gs.print(expirationDate);

or

var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var expirationDate = parsed.AssetWarrantyResponse.AssetEntitlementData[0].EndDate;
gs.print(expirationDate);

 

Hello Nayan, 

I changed the script to reflect your recommendations but now I am receiving an error message of "Cannot read property "0" from undefined. 

find_real_file.png

My Mistake, AssetWarrantyResponse is also array of object.

Try like this - 

var parser = new JSONParser();
var parsed = parser.parse(responseBody);

var expirationDate = parsed.AssetWarrantyResponse[0].AssetEntitlementData[0].EndDate;

gs.print(expirationDate);