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

Hi Sourabh,

SCRIPT INCLUDE CODE:

var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseObject = JSON.stringify(responseBody);
return responseObject;

 

CLIENT SCRIPT CODE:

function responseParser(answer){
var result = answer.responseXML.getElementsByTagName("result");
result = JSON.parse(result);
g_form.setValue('aadhar_details', result.client_id);

}

Regards,

Prudhvi

Ankur Bawiskar
Tera Patron
Tera Patron

@Prudhvi Yerriboyina 

update as this in client script

you should go to the data object as well

function responseParser(answer){
    var result = answer.responseXML.getElementsByTagName("result");
    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

Hi Ankur,

 g_form.setValue('aadhar_details', result.data.client_id) is not setting the value to filed and moreover looks like code getting terminated at JSON.parse(result) point.

Reagrds,

Prudhvi

Hi,

did you add alert to check what comes in answer?

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

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

Yes, 

alert shows as "[object HTMLCollection]" but after this step the code not getting parsed.

Regards,

Prudhvi