Parse response

Kumar38
Kilo Sage

I have a response from target system and need to fetch the status from it and it seems to contain a lot of u0022 in it . How can I fetch the status and its values from the below payload.

 

{"attributes":{"appType":"Connected","message":"Failed to get server response. Response returned: {\u0022type\u0022:\u0022error\u0022,\u0022status\u0022:409,\u0022code\u0022:\u0022user_login_already_used\u0022,\u0022help_url\u0022:\u0022http:\\/\\/developers.box.com\\/docs\\/#errors\u0022,\u0022message\u0022:\u0022User with the specified login already exists\u0022,\u0022request_id\u0022:\u001340hfdcheqmi8u4fk\u0022}"},"complete":false,"errors":["Status : failed\nAn unexpected error occurred: appname.tools.GeneralException: The application script threw an exception: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 BSF info: script at line: 0 column: columnNo\n"],"failure":false,"metaData":null,"requestID":null,"retry":false,"retryWait":0,"status":null,"success":false,"warnings":null}
1 ACCEPTED SOLUTION

Harshal Aditya
Mega Sage
Mega Sage

Hi @Kumar38 ,

 

Please try -

var obj = {"attributes":{"appType":"Connected","message":"Failed to get server response. Response returned: {\u0022type\u0022:\u0022error\u0022,\u0022status\u0022:409,\u0022code\u0022:\u0022user_login_already_used\u0022,\u0022help_url\u0022:\u0022http:\\/\\/developers.box.com\\/docs\\/#errors\u0022,\u0022message\u0022:\u0022User with the specified login already exists\u0022,\u0022request_id\u0022:\u001340hfdcheqmi8u4fk\u0022}"},"complete":false,"errors":["Status : failed\nAn unexpected error occurred: appname.tools.GeneralException: The application script threw an exception: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 BSF info: script at line: 0 column: columnNo\n"],"failure":false,"metaData":null,"requestID":null,"retry":false,"retryWait":0,"status":null,"success":false,"warnings":null};

//var jsonobj = JSON.parse(obj);
var split = obj["attributes"]["message"].split('"status":')[1];
var status = split.split(",")[0];
gs.info(status)

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

 

 

View solution in original post

2 REPLIES 2

Harshal Aditya
Mega Sage
Mega Sage

Hi @Kumar38 ,

 

Please try -

var obj = {"attributes":{"appType":"Connected","message":"Failed to get server response. Response returned: {\u0022type\u0022:\u0022error\u0022,\u0022status\u0022:409,\u0022code\u0022:\u0022user_login_already_used\u0022,\u0022help_url\u0022:\u0022http:\\/\\/developers.box.com\\/docs\\/#errors\u0022,\u0022message\u0022:\u0022User with the specified login already exists\u0022,\u0022request_id\u0022:\u001340hfdcheqmi8u4fk\u0022}"},"complete":false,"errors":["Status : failed\nAn unexpected error occurred: appname.tools.GeneralException: The application script threw an exception: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 BSF info: script at line: 0 column: columnNo\n"],"failure":false,"metaData":null,"requestID":null,"retry":false,"retryWait":0,"status":null,"success":false,"warnings":null};

//var jsonobj = JSON.parse(obj);
var split = obj["attributes"]["message"].split('"status":')[1];
var status = split.split(",")[0];
gs.info(status)

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

 

 

Manmohan K
Tera Sage

Hi @Kumar38 

 

You can use below code to get the status

var responseStr=JSON.stringify({"attributes":{"appType":"Connected","message":"Failed to get server response. Response returned: {\u0022type\u0022:\u0022error\u0022,\u0022status\u0022:409,\u0022code\u0022:\u0022user_login_already_used\u0022,\u0022help_url\u0022:\u0022http:\\/\\/developers.box.com\\/docs\\/#errors\u0022,\u0022message\u0022:\u0022User with the specified login already exists\u0022,\u0022request_id\u0022:\u001340hfdcheqmi8u4fk\u0022}"},"complete":false,"errors":["Status : failed\nAn unexpected error occurred: appname.tools.GeneralException: The application script threw an exception: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 BSF info: script at line: 0 column: columnNo\n"],"failure":false,"metaData":null,"requestID":null,"retry":false,"retryWait":0,"status":null,"success":false,"warnings":null});

var formattedResponse=JSON.parse(responseStr);

gs.print(formattedResponse.status);