I have a json data like below
{
"data": {
"points": 390,
"medal": "gold",
"activeGoals": [{
"examType": full,
"dateApplied": null,
"status": "Active-GoodStanding",
"term": 2,
"amountPaid": 2500,
"pointEarned": null,
"examDetails": {
"totalAmountPaid": 4500,
"examDate": 1459449000000,
"endDate": 1554057000000,
"totalMarks": 100,
"nextExamDate": 1493577000000,
"interestRate": 0
}
}]
}
}
In this Json data there is 'activeGoal' array,and inside of that there is an another array 'examDetails'
In Controller,I tried assigning these data into the angular object like below
examCtrl.details ={};
examCtrl.details =data;
examCtrl.activeGoals=data.activeGoals;
examCtrl.examDetails=data.activeGoals.examDetails;
I am getting activeGoals, but for examDetails I am getting 'undefined' error.
What is the wrong i am doing here??
assign like this : examCtrl.examDetails=data.activeGoals[0].examDetails;
since activeGoals is itself an array and you need first index([0]) value of it i.e., examDetails.
Further: as per your JSON examDetails is object not array.
if you're confused what the exactly the data is you can always convert in JSON editor.then you can easily assign the object from JSON data.
Related
I have an object which is given back through my REST API and I need to iterate through it for synchronizing a DB. So the object contains another object called tables. The tables object has different arrays with table names and their key value pairs.
I could not loop through the tables object about two days whatever I did and it is really annoying getting null or undefined values back.
For example I tried iterating through the table array with the JavaScript function object.forEach((article)=>console.log(article.id,article.name));
const obj = response.content.tables.article;
function findArticles(obj) {
obj.forEach((article)=>console.log(article.id,article.name));
}
I can't get any value back. When I try to console.log(response.content); it shows me everything. As soon as I try to output response.content.tables it says undefined.
This is the structure of the object response.content:
{
"status": "1",
"message": "sync out request successfull",
"tables": {
"article": [
{
"id": 1,
"name": "baseball"
},
{
"id": 2,
"name": "truck"
},
],
"food": [],
"animals: []
}
}
Try converting the response to an object using JSON.parse(xyz) before attempting to get the properties.
var xyz = '{ "status": "1", "message": "sync out request successfull", "tables": { "article": [{"id": 1,"name": "baseball"},{"id": 2,"name": "truck"}],"food": [],"animals": []}}'
var obj = JSON.parse(xyz);
$(obj.tables).each(function (ix, el) {
console.log(el)
});
I solved it like this:
var obj = response.content;
var JSON = JSON.parse(obj);
var articleTable = JSON.tables.article;
articleTable.forEach((article)=>console.log(article.id,article.name));
After I parsed the response.content object to JSON it was available to access the nested objects as 'tables' and 'article'. After passing the article object with the articleTable variable to the forEach it has been possible to access each elements. Now I get results.
I really appreciate your help
T3.0 it wasn't able to solve the problem without you.
My JSON data returned via API call:
{
"edit_date": "2019-05-27 15:50:03",
"active": 1,
"form_uuid": "e5ae0a15-3914-4e1d-a356-6490369c5d0b",
"staff_uuid": "",
"regarding_object": "job",
"regarding_object_uuid": "22ce9784-8f03-45dc-9d4b-c62a85fb2adb",
"field_data": "[{\"FieldType\":\"Multiple Choice\",\"Question\":\"Retest In\",\"Response\":\"6 Months\",\"UUID\":\"113fc0c0-6d6d-443e-970d-4fc6ea9044c1\",\"SortOrder\":10},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Insulation Resistance\",\"Response\":\"Pass\",\"UUID\":\"63100366-0dc8-4787-805e-db8a2a90b07b\",\"SortOrder\":5},{\"FieldType\":\"Multiple Choice\",\"Question\":\"PAT Result\",\"Response\":\"Pass\",\"UUID\":\"9f9ed288-c56d-4b39-88dc-a1a9941c65ab\",\"SortOrder\":9},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Condition\",\"Response\":\"Good\",\"UUID\":\"d6f0c0d8-7935-4292-8d79-38e4bf70454b\",\"SortOrder\":3},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Appliance Type\",\"Response\":\"Portable Appliance\",\"UUID\":\"ca94ec70-74da-4432-a1e8-3c2c040ecb8d\",\"SortOrder\":1},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Earth Resistance\",\"Response\":\"N\\/A\",\"UUID\":\"45a46b64-b28b-4f0a-b86e-a73f2742892b\",\"SortOrder\":4},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Leakage Current\",\"Response\":\"Pass\",\"UUID\":\"2d492371-ba9f-4fb6-a1a6-9c978549479b\",\"SortOrder\":6},{\"FieldType\":\"Multiple Choice\",\"Question\":\"Appearance Changed\",\"Response\":\"No\",\"UUID\":\"9a4f3b3d-40b9-4038-b0c5-e5866a607f71\",\"SortOrder\":7}]",
"timestamp": "2019-05-27 15:50:02",
"form_by_staff_uuid": "8b3031c3-469f-48f4-9e46-daa8fe01ccfb",
"document_attachment_uuid": "",
"asset_uuid": "b6420210-0709-4a4c-956b-3d7d4575d00a",
"uuid": "198fc098-0271-4f28-be4c-1a2fb5066dfa"
}
How can I index the nested field_data array?
I am chasing the "Response" object where "Question" = "Retest In"
I don't understand what all the backslashes are there for and I can't seem to call the object value
myJSONdata.field_data[0].Response
I'm building a ReactJS web application, I have a JSON array:
[{
"2149166938": {
"name": "Classical-42",
"icon": "/common/destiny2_content/icons/e6885dab15cafeaf0dbf80f1ff9b5eb8.jpg"
},
"2149166939": {
"name": "Countess SA/2",
"icon": "/common/destiny2_content/icons/de8906743db9fce201b33802c8498943.jpg"
},
"2154059444": {
"name": "The Long Goodbye",
"icon": "/common/destiny2_content/icons/fe07633a2ee87f0c00d5b0c0f3838a7d.jpg"
}
}]
When I make a call to an API I am returned a value of lets say 2154059444 which is the same value as the last object in my JSON file.
How would I iterate through the JSON file to find the object which has a value of 2154059444 and then access and render it's child values like name or icon?
you can do something like this. Your array is not proper please edit.
Create filtered data :
//here i am addding single dummy point you can make your function
l
et filteredData = [].concat(data.map(test => {
if(Object.keys(test)[0]==="2154059444"){
return test["2154059444"]
}
})).filter(Boolean)
and simply render it app like this .
{ filteredData.map(test => <div>{test.name}</div>)}
Here is live link
This is simple. JSON is equivalent to Javascript object. So you can do something like result[0]["2154059444"]. You might want to use JSON.parse for converting the JSON string to Javascript object.
I guess I'm stuck on stupid. I've been at this for the last few hours and can't seen to figure it out. Admittedly, I am new to ng/ionic2.
I am trying to loop through the response from my post request. I am getting a valid(validated online) big, fat JSON object from my own web api. It looks like this:
`"details": [{
"item_ID": "4",
"item_attribute_ID": "JiggyJamband_1_642",
"item_color_bool": "false",
"item_name": "Test Item 4",
"item_price": "18.95",
"item_desc": "4 This is a test of the ajax input",
"item_gender": "Girls"
},
{ ... },
"attributes": {
"JiggyJamband_1_642": [{
"color": "no-color",
"Xs": "80",
"Sm": "0",
"Med": "0",
"Lrg": "0",
"Xl": "0",
"Xxl": "10"
}],
"JiggyJamband_5_5664": [{
"color": "no-color",
"Xs": "0",
"Sm": "0",
"Med": "0",
"Lrg": "0",
"Xl": "0",
"Xxl": "50"
}],
{ ... }`
I am able to access individual "details" and "attributes" like this:
this.itemsDataService.getRemoteData(urlCode)
.subscribe(response => {
this.itemsJson = response;
this.dObj = this.boothItemsJson.details;
//this.aObj = this.boothItemsJson.attributes;
this.aObj = response["attributes"]["JiggyJamband_1_642"];
});
My provider looks like this:
getRemoteData(urlCode): any {
return this.http.post('http://localhost/process-fe-app/_itemJson.php', JSON.stringify(urlCode))
.map(res => res.json()); }
My question: items in details is dynamic and has an item-attribute_ID that is related to at least 1 entry in attributes. Entrys for attributes are dynamic as well - each item can have multiple attributes. The array keys of the individual attributes are the static (sizes and colors) and either have values or they don't. I need to be able to loop over the attributes object (aObj) of arrays and the arrays inside them. I do not need the ngFor or ngIf statements as this data won't be directly displayed per se. The json data is returned just fine but I just need to be able to access it call methods on the based on the data (like putting into storage with the attribute ID as the key "JiggyJamband: color: no-color, xs:50, s:100... etc"
What I've tried: this tutorial https://www.youtube.com/watch?v=0kHJgw6Li_4, and googling ever iteration of the wording for this problem I could think of.
Perhaps this sample iteration will help you get your head around it.
this.itemsDataService.getRemoteData(urlCode)
.subscribe(response => {
for(var k in response){
for(var k2 in response[k]){
console.log([k,k2,response[k][k2]]);
}
}
});
I have a website with JsRender and a JSON file.
My JSRender code:
{{for ~getModel(cards)}}
{{:id}}
{{/for}}
My JSON file:
{
"alpha": {
"cards": [{
"id": "alpha-01"
}, {
"id": "alpha-02"
}, {
"id": "alpha-03"
}]
},
"beta": {
"cards": [{
"id": "beta-01"
}, {
"id": "beta-02"
}]
}
}
In {{for ~getModel(cards)}} is cards, a suffix.
My JsRender helper concatenates a prefix and a suffix and the result is a string.
This string should be my array for the for loop. The suffix is a dynamic part, a parameter from the URL (?model=alpha)
The for loop should run through the array alpha.cards -> {{for ~getModel(alpha.cards)}}
But when I concate the dynamic part and the suffix, the return value is a string, and the for loop doesn't work. It seems the value (return) for the helper must be an array.
Is that right or is there a another solution for the problem?
Yes - you need to return an array. JsRender renders against javascript objects and arrays (typically a hierarchy of objects and arrays). If your string is a JSON string you need to convert/eval first to produce the corresponding object or array...