Getting a undefined error while assigning a variable the value from a json payload

AbineshM
Tera Contributor
var payload = JSON.parse(facilityPayload);
var dslDeviceName = (payload.enhSoiuShowServiceOrderReply.intDslAidNumber.dslDeviceName) ;
 
so if enhSoiuShowServiceOrderReply.intDslAidNumber.dslDeviceName is not present in the payload i am getting a undefined error. I understand why i am getting the error. But not able to figure how to validate it.

i tried JSUtils.notNil it didnt wok. i Also tried using the following function 
function myFunction(obj1) {
            if (obj1 == null) {
                obj1 = "";
            } else if (typeof obj1 == undefined ) {
                obj1 = "";
            }
            return obj1;
        }
 
still not able to resolve it.
1 ACCEPTED SOLUTION

RaghavSh
Kilo Patron

Put your code in try catch block and then check.

try{
var payload = JSON.parse(facilityPayload);
var dslDeviceName = (payload.enhSoiuShowServiceOrderReply.intDslAidNumber.dslDeviceName) ;
}
catch(e){
}

Raghav
MVP 2023

View solution in original post

2 REPLIES 2

RaghavSh
Kilo Patron

Put your code in try catch block and then check.

try{
var payload = JSON.parse(facilityPayload);
var dslDeviceName = (payload.enhSoiuShowServiceOrderReply.intDslAidNumber.dslDeviceName) ;
}
catch(e){
}

Raghav
MVP 2023

Thanks @RaghavSh ..... it worked.