How to read from JSON in protractor test? - javascript

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"
]
}
]
}
}

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.

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.

Related

Vue js : Filter dynamic data

In my Vuejs below I want to filter the reviewed:true only questions, and get the length of them, but my code below gives an error TypeError: question.reviewed.includes is not a function ,is there a way to do it?
Here is the screenshot about the json file:
JSON File
filterReviewed() {
return this.questions.filter((question) => {
return (
question.reviewed
.includes('true')
);
});
},
includes is a method of Object of Array type. Directly judge attribute reviewed is OK
filterReviewed (){
return this.questions.filter((question) => question.reviewed);
}
To filter the reviews try this:
filterReviewed() {
return this.questions.filter((question) => question.reviewed === true);
},
To get the filterReviewed length try this:
filterReviewed.questions.length

Access JSON Array with Javascript?

I get an JSON array via an Ajax request. This one looks like this:
{
"data":{
"title":"Frau",
"academic_title":null,
"first_name":"Lynda",
"last_name":"McCrow",
"company":"Tekfly",
"street":"Sage",
"zip":"4860-077",
"country":"Portugal",
"city":"Quinta",
"phone":"6727086107",
"fax":"4941912651",
"mobile":"3722716317",
"email":"lmccrow7#newyorker.com",
"web":"shop-pro.jp",
"mailbox":"81-0982335",
"mailbox_country":"Indonesia",
"mailbox_zip":null,
"mailbox_city":"Susoh",
"birthday":"1977-02-11"
}
}
But I have no idea, how to access the JSON array. I already tried all of this:
success: function(data) {
console.log(data[0].data.title);
console.log(data[0].title);
console.log(data.data[0].title);
console.log(data.title);
}
Can you guys give me a hint?
Kind regards
You have tried everything except:
data.data.title
It's an Object and you need to use Object.key() or something to iterate. Technically, it's like this:
// Inside your function, this is what gets passed.
data = {
"data":{
"title":"Frau",
"academic_title":null,
"first_name":"Lynda",
"last_name":"McCrow",
"company":"Tekfly",
"street":"Sage",
"zip":"4860-077",
"country":"Portugal",
"city":"Quinta",
"phone":"6727086107",
"fax":"4941912651",
"mobile":"3722716317",
"email":"lmccrow7#newyorker.com",
"web":"shop-pro.jp",
"mailbox":"81-0982335",
"mailbox_country":"Indonesia",
"mailbox_zip":null,
"mailbox_city":"Susoh",
"birthday":"1977-02-11"
}
};
for (var key in data.data) {
console.log(`${key}: ${data.data[key]}`);
}
data is not an array, nor JSON, it's an object literal. Your last try comes close, but you need to access
data.data.title
or you could destructure data in the success param
success: function({data}) {
// now you could access data.title directly
console.log(data.title);
}

Pulling Out Value from Within Two-Levels Deep Object

I am trying to retrieve one particular value from within a two-levels deep object data structure. First off, though, I am saving into a variable within the function, like this:
getTargetId() {
if (this.authenticationService.isAuthenticated()) {
const userInfo = sessionStorage.getItem('currentUser');
console.log(userInfo);
}
}
From:
console.log(userInfo);
I get this back in the console:
{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}
What I want to do is specifically pull out the "_id" value here.
I tried:
console.log(userInfo.data._id);
But then my IDE is showing me an error:
'Property '_id' does not exist on type 'string'.
How do I dig out "_id" in this case?
You are accessing it wrong
Try userInfo.data._id
In the log of your object you can see by the {} notation that data is another object, so after accessing data you can access its properties just as you would with any other object.
I also see that you are getting
'Property '_id' does not exist on type 'string'.
This could mean that you never parsed the information. To find out if this is the case this should be right:
Running->
console.log(userInfo);
Returns->
{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"...}
Just after this code:
Running->
console.log(typeof userInfo);
Returns->
"string"
With your edits, I can see that this is the case.
Try:
userInfo = JSON.parse(sessionStorage.getItem('currentUser') );
console.log(userInfo.data._id);
The _id property is under the data key:
const response = {
"token":"sometoken.value",
"data": {
"_id":"8cd0362c0",
"phone":"555-4343"
}
};
console.log(response.data._id)
You can also use destructuring:
const { _id } = response.data;
console.log(_id)
or:
const { data: { _id }} = response;
console.log(_id);
So, as #jonsharpe pointed out, the key was to JSON.parse the string first. So this gets me the value I need for "_id":
getTargetId() {
if (this.authenticationService.isAuthenticated()) {
const userInfo = JSON.parse(sessionStorage.getItem('currentUser'));
console.log(userInfo.data._id);
}
}
Actually your string is returned as JSON string. So you have to parse it into object using JSON.parse() if you are using js or with $.parseJSON() if you are using Jquery. So your updated code now looks like this.
var user ='{"token":"sometoken.value","data":{"_id":"8cd0362c0", "phone":"555-4343"}}';
var k = JSON.parse(user);
alert(k.data._id);
And Fiddle is here.
Thank You

Parse.com include related field array on save

Is it possible to include related field on save i.e.
save(null, {
success: function(updatedObject) {
// Is there a way to include another field which is an array of pointers either here or before save()?
},
error: function() {
...
}
});
Only when you query for an item can you get included objects, by using the include method.
var query = new Parse.Query('myClass');
query.include('arrayOfPointersColumn');
query.first().then(function(result) {
// you can access the full parse objects inside the result object.
var otherObjects = result.get('arrayOfPointersColumn');
console.log(otherObjects);
});

How I can simplify this code? convert file json to array json

I have this json file, and I'm turning into a json array, everything works fine, but the real json file contains more data.
How to simplify this code could either change the javascript or json file.
{
"Subject1":{
"Biology":{
"Category":{
"Cell":{
"question1":{
"que1":"what's....?"
},
"question2":{
"que2":"what's....?"
},
"question3":{
"que3": "what's....?"
}
},
"Bactery":{
"question1":{
"que1":"what's....?"
},
"question2":{
"que2": "what's....?"
},
"question3":{
"que3": "what's....?"
}
}
}
}
}
}
this is my code witch convert the file json into array json of javascript:
var exa = [];
function show() {
$.getJSON('questions.json', function (json) {
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
exa.push({
//I want to simplify this part, because the real json file have more questions
que1: item.Biology.Category.Cell.question1.que1,
que2: item.Biology.Category.Cell.question2.que2,
que3: item.Biology.Category.Cell.question3.que3,
que11: item.Biology.Category.Bactery.question1.que1,
que12: item.Biology.Category.Bactery.question2.que2,
que13: item.Biology.Category.Bactery.question3.que3
});
}
}
//return Books;
console.log(exa)
})
}
Instead of que1, que2, etc., just have the questions in an array
"Cell": [
"what's this?",
"what's that?"
]
You can use json-path and write JsonPath.read(json, "$..question"); to get all questions. You need to change all que[*] in your file to question too.

Categories

Resources