Reputation: 13
{
"\/dom-12239-1-cover-band-2018-greg-goodloe":{
"server":"ia601507.us.archive.org",
"dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
"misc":{
"image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
"collection-title":"Denver Open Media"
}
}
}
This is my Json file, how can I get the image value by using JS? "/dom-12239-1-cover-band-2018-greg-goodloe" will not always same Thank you!
Upvotes: 1
Views: 77
Reputation: 42304
Because you don't know what the key is going to be, you need to loop over the JSON for each key. Then you can output the image based with json[key].misc.image
, as image
is inside of misc
.
This can be seen in the following:
var json = {
"\/dom-12239-1-cover-band-2018-greg-goodloe":{
"server":"ia601507.us.archive.org",
"dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
"misc":{
"image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
"collection-title":"Denver Open Media"
}
},
"another_key":{
"server":"ia601507.us.archive.org",
"dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
"misc":{
"image":"another_image",
"collection-title":"Denver Open Media"
}
}
};
for(var key in json) {
console.log(json[key].misc.image);
}
Hope this helps! :)
Upvotes: 0
Reputation: 5095
Edited for your requirement of changing field name.
for (var k in obj) {
var image = obj[k].misc.image;
}
Where obj
is your JSON object.
function mySolution(obj) {
for (var k in obj) {
var image = obj[k].misc.image;
console.log(image);
}
}
var obj = {
"\/dom-12239-1-cover-band-2018-greg-goodloe":{
"server":"ia601507.us.archive.org",
"dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
"misc":{
"image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
"collection-title":"Denver Open Media"
}
}
}
mySolution(obj);
obj = {
"\/dom-sdasdfasdfasdfasdf-1-cover-band-2018-greg-goodloe":{
"server":"ia601507.us.archive.org",
"dir":"\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe",
"misc":{
"image":"https:\/\/ia601507.us.archive.org\/33\/items\/dom-12239-1-cover-band-2018-greg-goodloe\/dom-12239-1-cover-band-2018-greg-goodloe.thumbs\/dom-12239-1-cover-band-2018-greg-goodloe_000001.jpg",
"collection-title":"Denver Open Media"
}
}
}
mySolution(obj);
Upvotes: 1