user3340037
user3340037

Reputation: 731

Accessing attributes of a json

I have this JSON output

  {  
   "status":1,
   "complete":1,
   "list":{  
      "792489954":{  
         "item_id":"792489954",
         "resolved_id":"792489954",
         "given_url":"http:\/\/www.liveathos.com\/?gclid=Cj0KEQiAzb-kBRDe49qh9s75m-wBEiQATOxgwZcJ5_ws34o4PUSUYDGqs8HEbLF-LyjxrTPOwn6AYV8aAmMk8P8HAQ",
         "given_title":"Athos - Wearable Technology for Fitness",
         "favorite":"0",
         "status":"0",
         "time_added":"1418754744",
         "time_updated":"1418754746",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":0,
         "resolved_title":"Wearable Technology for Fitness",
         "resolved_url":"http:\/\/www.liveathos.com\/?gclid=Cj0KEQiAzb-kBRDe49qh9s75m-wBEiQATOxgwZcJ5_ws34o4PUSUYDGqs8HEbLF-LyjxrTPOwn6AYV8aAmMk8P8HAQ",
         "excerpt":"Thank you for reserving Athos. You will receive a confirmation email with reservation details and a referral link where you get $10 off your next order.",
         "is_article":"0",
         "is_index":"0",
         "has_video":"0",
         "has_image":"0",
         "word_count":"25"
      },
      "692647226":{  
         "item_id":"692647226",
         "resolved_id":"692647226",
         "given_url":"http:\/\/www.terrafugia.com\/news",
         "given_title":"News | Terrafugia",
         "favorite":"0",
         "status":"0",
         "time_added":"1418754204",
         "time_updated":"1418754204",
         "time_read":"0",
         "time_favorited":"0",
         "sort_id":1,
         "resolved_title":"News",
         "resolved_url":"http:\/\/www.terrafugia.com\/news",
         "excerpt":"",
         "is_article":"0",
         "is_index":"1",
         "has_video":"0",
         "has_image":"0",
         "word_count":"0"
      },
      `...etc`      "since":1419641101
   };

It's a JSON with a list that should have article items on it. I want to access the properties of these smaller items like given_url.

I'm using

for key in (BIGJSONRESPONSE).list
do etc...

When I try printing the key, I get only the id that comes before an item. Any idea how to access more?

Thanks!

Upvotes: 0

Views: 51

Answers (2)

Mouser
Mouser

Reputation: 13304

This should be correct:

for in returns the keys of an object. Since the key refers to a property of the object one can access the property using [key].

for (key in (BIGJSONRESPONSE).list)
{
    console.log(BIGJSONRESPONSE.list[key]) // log the entry to console for debugger.
}

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49813

You asked for the keys, you got the keys. To get the item associated with that key, use BIGJSONRESPONSE.list[key].

Upvotes: 3

Related Questions