Parsing the JSON object

Prudhvi Raj Y
Tera Expert

Hello,

I am trying to parse the Rest Message response but not able to get the proper data out.

after executing the below code through REST Message.

var response = request.execute();
var requestBody = response.getBody();
var httpStatus = response.getStatusCode();
var parsedJSON = JSON.stringify(JSON.parse(requestBody));
gs.info("Today Testing: "+parsedJSON);
return parsedJSON; 

I can see the response message as below

{"data": {"client_id": "XXXXXXXX", "otp_sent": true,
"if_number": true, "valid_aadhaar": true}, "status_code": 200, "message_code": "success",
"message": "OTP Sent.", "success": true}

Now I am trying to parse the data and get the client_id to return back to client script but above script returning the undefined value. please suggest a way to get key value results.

Thanks,

Prudhvi

1 ACCEPTED SOLUTION

Hi,

that's the issue

since it's not a string it cannot be parsed by next line

update as this; change in bold

function responseParser(answer){
    var result = answer.responseXML.getElementsByTagName("answer");

alert(result); // what came here
    result = JSON.parse(result);
    g_form.setValue('aadhar_details', result.data.client_id);

}

Regards
Ankur

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

View solution in original post

14 REPLIES 14

Sourabh26
Giga Guru

Hi,

 

var response = request.execute();
var requestBody = response.getBody();
var httpStatus = response.getStatusCode();
var parsedJSON = JSON.stringify(JSON.parse(requestBody));
gs.info("Today Testing: "+parsedJSON);
return parsedJSON; 

//Assuming below is the parsedJSON value

/*
{"data": {"client_id": "XXXXXXXX", "otp_sent": true,
"if_number": true, "valid_aadhaar": true}, "status_code": 200, "message_code": "success",
"message": "OTP Sent.", "success": true}
*/

//So to get the client_id from it below is the code.
var obj = JSON.parse(requestBody);
var clientID = obj.data.client_id;

 

Mark this as Helpful/Correct, if Applicable.

 

Regards,

Sourabh

Richard Hine
Tera Guru
Tera Guru

Why would you label your response body as 'requestBody'? This would be incredibly confusing and not easy to maintain!

If you are using IntegrationHub, you could consider using a JSON Parser step, otherwise here is the code I would use...

var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var responseObject = JSON.parse(responseBody);
return responseObject.data.client_id; 

Hope this helps,

Richard

Hi Richard,

Thanks from the response. it is working fine but when I return the value to client script back it is placing the value still as object only([object HTMLCollection]) how can i parse the the value in client side?

Regards,

Prudhvi

Hi,

 

Share your client side code to see what you have written.

Although you need to simply stringify your object to send on the client side and then again parse it to get the related data from the object.

 

Regards,

Sourabh