Search code examples
jsonautomationprotractorwebstormweb-api-testing

How to extract string from JSON object in protractor


I am not able to extract string from JSON object in protractor in webstorm. I want value of statusCode i.e :200, The code I've tried for this mentioned below...

CODE IS :

console.log('body is :' + JSON.stringify(body));

var jsonResponse = JSON.stringify(response);

console.log('---------response is: '+ jsonResponse);

var statusResponse = jsonResponse.getString("statusCode");

console.log('---- statusResponse is :'+ statusResponse.toString());

OUTPUT IS:

body is :{"oBody":{"payLoad":{"sLoginId":"[email protected]","iInstId":4019,"sFirstName":"PRIYANKA","sLastName":"MADGUNDI","sEmail":"[email protected]","sEmpId":"HDB18458","sMobile":"8237276692","bActive":true,"aRoles":["DSA"],"aDealers":[{"id":"TW423684019","iInstId":4019,"iDlrId":"42368","sDlrName":"GODSPEED MOTORCYCLE PVT LTD","iBranchId":1278,"sBranchName":"HYDERABAD-SF","sProduct":"TW","bActive":true},{"oStatus":{"iStatus":200,"sStatus":"OK"}}

---------response is:

{"statusCode":200,"body":{"oBody":{"payLoad":{"sLoginId":"[email protected]","iInstId":4019,"sFirstName":"PRIYANKA","sLastName":"MADGUNDI","sEmail":"[email protected]","sEmpId":"HDB18458","sMobile":"8237276692","bActive":true,"aRoles":["DSA"],"aDealers","path":"/gonogo-api/atm/login-web-v3","href":"https://ssg.serviceurl.in/login-web-v3"},"method":"POST","headers":{"Postman-Token":"7aad048f-f8ad-4765-a0b8-be3b3a485ab6","cache-control":"no-cache","Content-Type":"application/json","accept":"application/json","content-length":380}}}

Solution

  • Try the below option

    var res= JSON.parse(response);
    console.log(res[0].statusCode);
    

    Hope it helps you