Access array in Json

Asked

Viewed 1,604 times

3

I’m trying to get a value inside JSON but I can’t get the value within date > Artist > image > [3]. #text

{
   "artist":{
      "name":"Madeon",
      "mbid":"fa1de503-aba7-41fa-a1ed-371b3e87a717",
      "url":"https://www.last.fm/music/Madeon",
      "image":[
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/34s/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":"small"
         },
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/64s/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":"medium"
         },
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/174s/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":"large"
         },
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/300x300/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":"extralarge"
         },
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":"mega"
         },
         {
            "#text":"https://lastfm-img2.akamaized.net/i/u/arQ/38c562652b32c8d8fe612c079a8f61ca.png",
            "size":""
         }
      ],
      "streamable":"0",
      "ontour":"1",
      "stats":{
         "listeners":"417989",
         "playcount":"7015758"
      }
   }
}

When I try to get the value with the following code:

data['artist']['image'].#text

I receive as a Undefined reply, how can I get the #text data?

1 answer

5


Use the bracket notation. Moreover, image is a array:

data.artist.image[3]['#text']

Click on View code snippet and then in Execute to see working:

var json = {
   "data":{
      "artist":{
         "name":"Madeon",
         "mbid":"fa1de503-aba7-41fa-a1ed-371b3e87a717",
         "url":"https://www.last.fm/music/Madeon",
         "image":[
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/34s/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":"small"
            },
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/64s/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":"medium"
            },
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/174s/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":"large"
            },
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/300x300/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":"extralarge"
            },
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":"mega"
            },
            {
               "#text":"https://lastfm-img2.akamaized.net/i/u/arQ/38c562652b32c8d8fe612c079a8f61ca.png",
               "size":""
            }
         ],
         "streamable":"0",
         "ontour":"1",
         "stats":{
            "listeners":"417989",
            "playcount":"7015758"
         }
      }
   }
};

console.log(json.data.artist.image[3]['#text']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.