We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

"JSONParser" is not defined, error while parsing JSON

Sushma Uday C
Tera Contributor

Hi all,

I am trying to parse JSON object and retrieve a token form JSON. And I am getting error as "JSONParser" is not defined.

Please find the below JSON and code for your reference.

JSON we are receiving

find_real_file.png

find_real_file.png

I want to retrieve the idToken form JSON. but I am getting an error as "JSONParser" is not defined.

 

Thanks in Advance,

Sushma Uday C

1 ACCEPTED SOLUTION

If responseBody1 is a JSON string you need to parse it initially like:

var responseObj1 = responseBody1;

var responseObj1Parsed = JSON.parse(responseObj1);

 

Then you can assess the token using:

var token = responseObj1Parsed.token.idToken.toString();

 

Geoff

View solution in original post

13 REPLIES 13

I have tested and this works:

 

var responseObj1 = {"message":"Successfully Logged in","token":{"idToken":"eyJhbGciOiJSUzI1NiIsImtpZCI6ImFiMGNiMTk5Zjg3MGYyOGUyOTg5YWI0ODFjYzJlNDdlMGUyY2MxOWQiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiTWljaGFlbCBTY290dCIsInJvbGUiOiJhZG1pbiIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9jYXJlYXItcGxheWdyb3VuZCIsImF1ZCI6ImNhcmVhci1wbGF5Z3JvdW5kIiwiYXV0aF90aW1lIjoxNjIzMDU3NTY2LCJ1c2VyX2lkIjoiSFdDYk9MN3I0MGFiRWhSc0JydXNhUnNId2d3MiIsInN1YiI6IkhXQ2JPTDdyNDBhYkVoUnNCcnVzYVJzSHdndzIiLCJpYXQiOjE2MjMwNTc1NjYsImV4cCI6MTYyMzA2MTE2NiwiZW1haWwiOiJtaWNoYWVsQG9mZmljZS5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsibWljaGFlbEBvZmZpY2UuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.ZQT3WtxHfayhqYJah0QyTrkZFjfseomcMtPXYY8T-j59CfS05K_mivOgsK8M11PKdWvZMWfxQxIY7_odQFZOTu2MP5wa2PoqvC0GVItbj5OcLTRvo6D3CuP-iIN7QHi9yRpyLTW2Xz4OYEyQA6w4_qegMJK7YTn2bDiAXa7HYyHBGsloW5dEAwX19rH-EB2HpFJfYZKdu424oRo8RMUo0LnNBuYkXwVPvikMq-Vx98ACPvZV42EkVAn2ZZcXHHA_WFmypxUi0_o4RJG6XCV_auj7H3ok1BKbobAnHu_AC2iXjnKL5z1C73Fz_Pjzvx8TdVNqw74ERTj6Xm5ioC5Nvg","refreshToken":"AGEhc0CuzXiyYZgE7ybXLC_RFgtpZrEmB4MxayAQBm7NYBx8ahHJfMo-iyms8o9Fns8VToqP0O6o8veDfLPN6gACKefiYj0uEdKdDfN9IW7ossUjS79Vaqk4P72qN3ENkUBiBHi8MmljpDEN4o5d4CqCUnQDwlwYzWP7ZFrj8YJW3vyImhjN0jza3-HrXv7534Q5RbptUPFDLpzVOZtdKnIIu_lZuyOQyaVcWhnx-gJb5QoSkpdy8Rw"},"expiresIn":"3600"}


var idToken = responseObj1.token.idToken.toString();
gs.log("token is " + idToken);

 

Geoff

As I am receiving in JSON format and it is dynamically populated, IF I just assign like this

var responseObj1 = responseBody1;

var token = responseObj1.token.idToken.toString();

 

I am getting an error as Cannot read property "idToken" from undefined

Hi,

this worked well for me

var str = '{"message":"Successfully Logged in","token":{"idToken":"eyJhbGciOiJSUzI1NiIsImtpZCI6ImFiMGNiMTk5Zjg3MGYyOGUyOTg5YWI0ODFjYzJlNDdlMGUyY2MxOWQiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiTWljaGFlbCBTY290dCIsInJvbGUiOiJhZG1pbiIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9jYXJlYXItcGxheWdyb3VuZCIsImF1ZCI6ImNhcmVhci1wbGF5Z3JvdW5kIiwiYXV0aF90aW1lIjoxNjIzMDU3NTY2LCJ1c2VyX2lkIjoiSFdDYk9MN3I0MGFiRWhSc0JydXNhUnNId2d3MiIsInN1YiI6IkhXQ2JPTDdyNDBhYkVoUnNCcnVzYVJzSHdndzIiLCJpYXQiOjE2MjMwNTc1NjYsImV4cCI6MTYyMzA2MTE2NiwiZW1haWwiOiJtaWNoYWVsQG9mZmljZS5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsibWljaGFlbEBvZmZpY2UuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.ZQT3WtxHfayhqYJah0QyTrkZFjfseomcMtPXYY8T-j59CfS05K_mivOgsK8M11PKdWvZMWfxQxIY7_odQFZOTu2MP5wa2PoqvC0GVItbj5OcLTRvo6D3CuP-iIN7QHi9yRpyLTW2Xz4OYEyQA6w4_qegMJK7YTn2bDiAXa7HYyHBGsloW5dEAwX19rH-EB2HpFJfYZKdu424oRo8RMUo0LnNBuYkXwVPvikMq-Vx98ACPvZV42EkVAn2ZZcXHHA_WFmypxUi0_o4RJG6XCV_auj7H3ok1BKbobAnHu_AC2iXjnKL5z1C73Fz_Pjzvx8TdVNqw74ERTj6Xm5ioC5Nvg","refreshToken":"AGEhc0CuzXiyYZgE7ybXLC_RFgtpZrEmB4MxayAQBm7NYBx8ahHJfMo-iyms8o9Fns8VToqP0O6o8veDfLPN6gACKefiYj0uEdKdDfN9IW7ossUjS79Vaqk4P72qN3ENkUBiBHi8MmljpDEN4o5d4CqCUnQDwlwYzWP7ZFrj8YJW3vyImhjN0jza3-HrXv7534Q5RbptUPFDLpzVOZtdKnIIu_lZuyOQyaVcWhnx-gJb5QoSkpdy8Rw"},"expiresIn":"3600"}';

var parser = JSON.parse(str);

var token = parser.token.idToken.toString();
gs.info("token is " + token);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

If responseBody1 is a JSON string you need to parse it initially like:

var responseObj1 = responseBody1;

var responseObj1Parsed = JSON.parse(responseObj1);

 

Then you can assess the token using:

var token = responseObj1Parsed.token.idToken.toString();

 

Geoff

Thanks @Geoff  and @Ankur Bawiskar  for your help. It Worked.