This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am trying to traverse a JSON file using a variable that gets set earlier in my script.
Example of the JSON:
{
"page1": [{
"template": "temp1",
"other": "example"
}],
"page2": [{
"template": "temp2",
"other": "example"
}],
"page3": [{
"template": "temp3",
"other": "example"
}]
}
The variable can be one of three items... page1, page2, page3
As I said I am setting the variable further up in the code so say the variable is:
var getid = "page1";
What I want to do is, if the variable is "page1", then get the "template" value of "temp1".
my console.log
console.log(json[getid]);
returns
[{template: "temp1"}]
but I don't really know how to go any further. Everything I try returns "undefined" or an error.
As I said, I want to get "temp1" in this example.
Can anyone help me along?
If you need more info, please let me know.
Thanks,
var json = {
"page1": [{
"template": "temp1",
"other": "example"
}],
"page2": [{
"template": "temp2",
"other": "example"
}],
"page3": [{
"template": "temp3",
"other": "example"
}]
};
var getid = "page1";
console.log(json[getid][0].template);
you are very close to the solution, just access object inside array you are done.
json[getid][0].template
will do
You can use the code below. json[getid] return an array of one object So you need to access the first item of array using [0] which will return object object and then use .template
let json = {
"page1": [{
"template": "temp1",
"other": "example"
}],
"page2": [{
"template": "temp2",
"other": "example"
}],
"page3": [{
"template": "temp3",
"other": "example"
}]
}
var getid = "page1";
console.log(json[getid][0].template);
Related
I have a json array which im getting my react data from it,
the json is like this:
{
"Folders": [
{
"name": "parent 2",
"children": [ //this is children_1
{
"name": "parent 2",
"id": "parent 2",
"children": [] //this is children_2
}
],
"id": 1
}
]
}
lets say i have the key value of name inside children(children_1) and i want to get the rest of the data inside that children using the name that i have, is there a way to do that ?
Look at jsonpath
so it will be
var json = require('jsonpath');
var names = jp.query(json, '$.Folders[*].children[*].children');
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I don't understand how I can access an value in a object that is an array with an object inside itself.
I've tried with the dot notation and [] and array.reduce. But I'm doing something wrong.
I've changed the values but the structure remains the same:
"test": {
"title": "My title",
"category": null,
"info": [{
"time": 10,
"type": "minutes"
}]
}
I need to get the values of time and type, but I get undefined.
The final object is first element of array so you need to first access its first element.
const obj = {
"test": {
"title": "My title",
"category": null,
"info": [{
"time": 10,
"type": "minutes"
}]
}
}
console.log(obj.test.info[0].time);
console.log(obj.test.info[0].type);
Use the dot notation to access the properties
Object a consists of the test object which in-turn contains the key info whose value is an array containing one object with the required keys
a->test->info->[{time,type}]
var a = {
"test": {
"title": "My title",
"category": null,
"info": [{
"time": 10,
"type": "minutes"
}]
}
}
console.log(a.test.info[0].time)
console.log(a.test.info[0].type)
I have a (nested) data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?
{
"data": [{
"name": "name1",
"value": "value1",
"list": [{
"sname": "sname1",
"svalue": "svalue1"
}, {
"sname": "sname2",
"svalue": "svalue2"
}]
}]
}
jQuery
var pk = $("#pk").val();
console.log(pk);
url = "/register/search?id=" + pk;
console.log(url);
$('#largeTable').DataTable({
"ajax": url,
"bDestroy": true,
"columns": [{
"data": "name"
},
{
"data": "value"
},
{
"data": "list.1.sname"
},
{
"data": "list.1.svalue"
},
{
"data": null,
"defaultContent": editview
}
]
});
Here it is possible to display either first or second list values by using list.1 or list.0
But I want two values at a time.
Also, how could I access the svalue of the second item in list?
I tried with data.list[1] but:
TypeError: data.list is undefined
Since data is an array, you should first get the item - and since you only have one item - you'd use data[0], and then get access to the list property like data[0].list[1] - this will give you the second item in the list - but since you are interested in a specific property (svalue) of this item, you will then access it like this: data[0].list[1].svalue.
A better approach would be to loop through the items in the data array - and then for each item, loop through the items in the list array. See #Rajesh's comment.
I hope that helps;
Specifically you can access it like this object.data[0].list[1].svalue. The reason data.list is undefined is because data corresponds to an array data: [{ }] this is why we use data[0], but data itself is a key in an object so before you can get to data you need to access it. If the objects name where data resides were object (like I did below) then it'd be accessed like this object.data[0]
const object = {
"data": [{
"name": "name1",
"value": "value1",
"list": [{
"sname": "sname1",
"svalue": "svalue1"
}, {
"sname": "sname2",
"svalue": "svalue2"
}]
}]
}
console.log(object.data[0].list[1].svalue)
I am iterating over this in my html file to create a dynamic table header.
I have a nested object
$scope.tasks = [{ "Number": 159232, "Title": "BUG", "Status": "pending", "Link": "www.google.com", "Card": "www.kanban.com", "Point": { "Value": 1, "IsTimeBased": true }, "ApprovalStatus":{ "CR": true , "BA": true } };
$scope.titles = Object.keys($scope.tasks[0]);
This iterates through all the non nested items but I cant figure out how to add the keys from the nested object.
I tried
$scope.titles.push(Object.keys($scope.tasks.ApprovalStatus[0]));
But nothing seems to be working I tried a splice in this fashion as well.
Instead of using .push use .concat.
$scope.titles = $scope.titles.concat(Object.keys($scope.tasks.ApprovalStatus[0]));
You can also do the concat for multiple args simultaneously.
I am getting a JSON in response from server:
{
"width": "765",
"height": "990",
"srcPath": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_MERGED_/1273.pdf",
"coverPage": "",
"documents": [
{
"index": "1",
"text": "Archiving Microsoft® Office SharePoint® Server 2007 Data with the Hitachi Content Archive Platform and Hitachi Data Discovery for Microsoft SharePoint",
"type": "doc",
"id": "HDS_054227~201106290029",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_2.png"
}
]
},
{
"index": "11",
"text": "Brocade FCoE Enabling Server I/O Consolidation",
"type": "doc",
"id": "HDS_053732~201105261741",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_2.png"
}
]
}
]
}
And I want to get pagelocation of the children.
Can anyone tell me how to do this?
Hi
i also want to get indexes from this and then want to get pagelocations of that particular children. Can you tell me how would i do that?
And also when i when i am getting indexes array it is returning me ,, only and not the index nos.
I am using following code for that :
indexes=response.documents.map(function(e){ return e.children.index; })
Thanks & Regards
If you're interested in simply retrieving all the page locations, you can do it using filter:
var locations = [];
json.documents.forEach(function(e,i) {
e.children.forEach(function(e2,i2) {
locations.push(e2.pageLocation);
)}
});
// returns flat array like [item1,item2,item3,item4]
You can get an array of arrays using map:
var locations = [];
var locations = json.documents.map(function(e) {
return e.children.map(function(e2) {
return e2.pageLocation;
});
});
// returns 2-dimensional array like [[item1,item2],[item1,item2]]
Your json response is an appropriate javascript object So you can access all elements of the object like you do as in back end.
here, you have an array of object of the type documents and each document object has array of objects of the type children. so
syntax would be
myjson.documents[0].children[0].pagelocation
( = http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png)
will give you the very first page location..
and so on