Sanja Paskova
Sanja Paskova

Reputation: 1110

How to read from JSON in protractor test?

I would like to read groups_id_ss for specific id.
How to do that in this json?

I successfully read response.docs , but then can't reach id and groups_id_ss.

{
 "responseHeader":{
      "status":0,
      "QTime":0,
      "params":{
         "q":"object_type_s:USER",
         "indent":"true",
         "wt":"json"
      }
   },
   "response":{
      "numFound":13,
      "start":0,
      "docs":[
         {
            "id":"sanja",
            "groups_id_ss":[
               "COORDINATION AO",
               "ACM_INVESTIGATOR_DEV"
            ]
         },
         {
            "id":"sanjaseconduser",
            "groups_id_ss":[
               "ACM_SUPERVISOR_DEV",
               "CHAIRMAN",
               "ACM_ADMINISTRATOR_DEV",
               "CPC INITIATOR",
               "COORDINATION AO",
               "ACM_INVESTIGATOR_DEV"
            ]
         }
      ]
   }
}

Upvotes: 3

Views: 2434

Answers (2)

krishnarajanr
krishnarajanr

Reputation: 696

Since you are using the values in separate file, you can use like this. I am assuming the file is stored as .json For example, your json values are in response.json

You can write code like this in your spec file:

var responseData =  require('../../response.json');

describe('some function description', function(){
   var groupID = responseData.response.docs[0].groupid_ss[0];
   var groupID = responseData.response.docs[0].groupid_ss[0]

});

Since that is json array, we have to give the index of the array in that. Hope this would work for you.

Upvotes: 2

As stated you could read response.docs from json. Let's say

var docs = response.docs;

As docs is an array you can use forEach to loop thruogh each element

docs.forEach(function(doc) {
if(doc.id === "desired_Id") {
    var groupIdSS = doc.groups_id_ss;
}
});

This way you can read groups_id_ss for desired id. Hope it helps.

Upvotes: 2

Related Questions