- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 03:11 AM
Am getting response as below line,
"{\"Table\":[[\"Yes\"],null]}"
And it contains Yes, if Yes is present in response i need to do something, how to parse or split the response to identify Yes or No present in it?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 03:39 AM
Hi Swathi,
Here is the code. I have executed in background and value is coming as Yes
you can also test
var str = '{"Table":[["Yes"],null]}';
var parsed = new JSONParser();
var parsedData = parsed.parse(str);
gs.print('Value is:'+parsedData.Table[0]);
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 05:36 AM
hi Srinivas,
var k=unescape("{\"Table\":[[\"Yes\"],null]}");
var json=new JSON().decode(k);
if(json.Table[0]=='Yes')
{
// write your code
}
When i write the above code it will work, k is hard coded...
But when i try with
var k=unescape(responseBody);
var json=new JSON().decode(k);
if(json.Table[0]=='Yes')
{
// write your code
}
it is not working....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 05:54 AM
Hi Swathi,
Just try using the script I provided. Even with the backslash being kept in the json string the script is able to fetch the value i.e. "Yes".
var k = "{\"Table\":[[\"Yes\"],null]}";
var parsed = new JSONParser();
var parsedData = parsed.parse(k);
gs.print('Value is:'+parsedData.Table[0]);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 06:07 AM
Hi Ankur,
You are hard coding the response body, so it is working for you, when you set value to K like
var K= responseBody;
Then it will not work.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 06:11 AM
Hi Swathi,
Could you share your complete script? Because you only mentioned that the response contains this exact string "{\"Table\":[[\"Yes\"],null]}"
May be you can add log statements and check
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2017 06:13 AM
try {
gs.log("Inside try");
var r = new sn_ws.RESTMessageV2('Test6', 'get');
r.setStringParameter('PWD', 'user_password');
r.setStringParameter('USERID', 'user_name');
r.setStringParameter('Token', '1');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);
var response = r.execute();
var parser = new JSONParser();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(httpStatus);
gs.log(responseBody);
var parsed = new JSONParser();
var parsedData = parsed.parse(responseBody);
gs.log('Parse Data:'+parsedData);
gs.log('data:'+parsedData.Table[0]);
if(parsedData.Table[0]=='Yes')
{
gs.log("Result is correct");
var user1 = GlideUser;
return user1.getUser(userName);
}
else
return;
}
catch(ex) {
var message = ex.getMessage();
}