1

I'm pulling my hair out a bit at the moment. I can't seem to figure out how to access the "media" content within the following json object using Jade.

   {
      "summary":"Jose Mourinho names his Real Madrid side to face Borussia Dortmund in the Champions League semi-final 24 hours early.",
      "type":"STY",
      "lastUpdated":"2013-04-23T16:31:39+00:00",
      "firstCreated":"2013-04-23T16:31:39+00:00",
      "hasShortForm":true,
      "media":{
         "images":{
            "index":{
               "67193384":{
                  "height":261,
                  "width":464,
                  "href":"http://thesun.co.uk/media/images/67193000/jpg/_67193384_67193383.jpg",
                  "altText":"Jose Mourinho"
               }
            }
         }
      },
   },

I can access summary, type, updated etc. But i cannot figure out how to access the image meta data within media.images.index.67193384

for item in results
    p #{item.summary}
    p #{item.lastUpdated}
    p #{item.media[0]} // ???

Can someone please help me figure out? I've never tried to access data that's an object within an object within an object. Also, the 67193384 object within images.index is unique and will always be different from result to result.

Thanks!

Scotty
  • 2,635
  • 6
  • 30
  • 39
  • Are you making the JSON? It doesn't look right. Images is not an array. Also, using "67193384" as a key isn't helpful. – Ross Apr 25 '13 at 08:08

2 Answers2

2

Bit of a hack, but it works:

- if (item.media && item.media.images)
  p #{item.media.images.index[Object.keys(item.media.images.index)[0]].height}
robertklep
  • 198,204
  • 35
  • 394
  • 381
0
for item in results
    p= item.summary
    p= item.lastUpdated
    - for (var key in item.media.images) {break;}
    p= item.images.index[key].height

The for loop is used to get your key.

Community
  • 1
  • 1
Ross
  • 14,266
  • 12
  • 60
  • 91
  • I seem to be getting the following error: `Cannot read property 'index' of undefined`. Which is a different error to @robertklep's solution :/ – Scotty Apr 25 '13 at 08:23
  • Is there a check we can do to check if that data exists, incase it's missing for a particular item? – Scotty Apr 25 '13 at 08:34
  • `- console.log(dataToCheck)` ? – Ross Apr 25 '13 at 12:16