how to check if a string is present in JSON response?

swathigangadhar
Tera Expert

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?

1 ACCEPTED SOLUTION

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


View solution in original post

17 REPLIES 17

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....


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


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.....


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


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();


  }