1
{  
   "\/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!

1
  • 2
    Please edit your question title to something meaningful. Removing the redundant tag JSON leaves Basic questions, which has absolutely no indication of what you're asking. Your title should describe a problem or question in a way that will convey meaning to future readers here seeing it in a list of search results. Thanks. Commented Jan 31, 2018 at 23:12

2 Answers 2

1

Solution

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.

Proof

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);

Sign up to request clarification or add additional context in comments.

2 Comments

I have many images, and \/dom-12239-1-cover-band-2018-greg-goodloe will change on every image.
@JiangZhan Updated my solution. Sorry about missing the requirement initially.
0

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! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.