Carl
Carl

Reputation: 543

How can I access an object in a complex JSON file as follows?

I am trying to access the events object array found in the JSON file gotten from this API: http://app.ticketmaster.com/discovery/v1/events.json?keyword=Madonna&apikey=fU1iKOy83W5ewGCIGQmW8I6FIPvOG8de

I have tried accessing the events object using this following logic :

   useEffect(() => {
    const fetchData = async () => {
      setIsLoading(true);
    const result = await axios(url);

    setData(result.data.events);
    setIsLoading(false);
    };

    fetchData();
  }, [url]);

<Button onClick={() =>
            setUrl(`http://app.ticketmaster.com/discovery/v1/events.json?keyword=${query}&apikey=fU1iKOy83W5ewGCIGQmW8I6FIPvOG8de`)
          }
            className={classes.button} color="primary">Search</Button>

Here is the JSON format in which the document appears

{  
   "_links":{  },
   "_embedded":{  
      "events":[  
         {  
            "name":"Kool 94.5 Presents - Ladies 80's with a Performance By So Madonna",
            "locale":"en-us",
            "eventUrl":"https://www.ticketweb.com/event/kool-945-presents--the-van-buren-tickets/9442495?REFERRAL_ID=tmfeed",
            "dates":{  
               "start":{  
                  "dateTime":"2019-08-11T03:30:00.000+0000",
                  "localDate":"2019-08-10",
                  "localTime":"20:30:00"
               },
               "end":{  
                  "dateTime":"2019-08-11T03:30:00.000+0000",
                  "localDate":"2019-08-10",
                  "localTime":"20:30:00"
               },
               "timezone":"America/Phoenix",
               "displayOptions":{  
                  "range":{  
                     "localStartDate":"2019-08-10",
                     "localEndDate":"2019-08-10"
                  }
               },
               "status":{  
                  "code":"active"
               }
            },
            "test":false,
            "_links":{  
               "self":{  
                  "href":"/discovery/v1/events/290056A107C86EEE?locale=en-us&domain=ticketmaster.com"
               },
               "categories":[  
                  {  
                     "href":"/discovery/v1/categories/10001?locale=en-us&domain=ticketmaster.com"
                  },
                  {  
                     "href":"/discovery/v1/categories/1?locale=en-us&domain=ticketmaster.com"
                  }
               ],
               "attractions":{  
                  "href":"/discovery/v1/attractions/2642724?locale=en-us&domain=ticketmaster.com"
               },
               "venue":{  
                  "href":"/discovery/v1/venues/205185?locale=en-us&domain=ticketmaster.com"
               }
            },
            "id":"290056A107C86EEE",
            "_embedded":{  
               "venue":[  
                  {  
                     "name":"The Van Buren",
                     "marketId":[  
                        36
                     ],
                     "country":{  
                        "countryCode":"US"
                     },
                     "state":{  
                        "stateCode":"AZ"
                     },
                     "city":{  
                        "name":"Phoenix"
                     },
                     "location":{  
                        "latitude":"33.451085000",
                        "longitude":"-112.079228000"
                     },
                     "postalCode":"85003",
                     "address":{  
                        "line1":"Van Buren",
                        "line2":"Phoenix, AZ"
                     },
                     "timeZone":"America/Phoenix",
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/venues/205185?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"205185",
                     "type":"venue"
                  }
               ],
               "categories":[  
                  {  
                     "name":"Music",
                     "level":1,
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/categories/10001?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"10001",
                     "type":"category"
                  },
                  {  
                     "name":"Rock and Pop",
                     "level":2,
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/categories/1?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"1",
                     "type":"category"
                  }
               ],
               "attractions":[  
                  {  
                     "url":"/Kool-945-Presents-Ladies-80s-tickets/artist/2642724",
                     "name":"Kool 94.5 Presents - Ladies 80's with a Performance By So Madonna",
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/attractions/2642724?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"2642724",
                     "type":"attraction"
                  }
               ]
            },
            "type":"event"
         },
         {  
            "name":"Makeup & Mimosas: Madonna Tribute Show!",
            "locale":"en-us",
            "eventUrl":"http://concerts.livenation.com/event/1C0056DAC6666433",
            "promoterId":[  
               653
            ],
            "dates":{  
               "start":{  
                  "dateTime":"2019-08-18T18:00:00.000+0000",
                  "localDate":"2019-08-18",
                  "localTime":"11:00:00"
               },
               "end":{  
                  "dateTime":"2019-08-18T18:00:00.000+0000",
                  "localDate":"2019-08-18",
                  "localTime":"11:00:00"
               },
               "timezone":"America/Los_Angeles",
               "displayOptions":{  
                  "range":{  
                     "localStartDate":"2019-08-18",
                     "localEndDate":"2019-08-18"
                  }
               },
               "status":{  
                  "code":"active"
               }
            },
            "test":false,
            "_links":{  
               "self":{  
                  "href":"/discovery/v1/events/1C0056DAC6666433?locale=en-us&domain=ticketmaster.com"
               },
               "categories":[  
                  {  
                     "href":"/discovery/v1/categories/10002?locale=en-us&domain=ticketmaster.com"
                  },
                  {  
                     "href":"/discovery/v1/categories/51?locale=en-us&domain=ticketmaster.com"
                  }
               ],
               "attractions":{  
                  "href":"/discovery/v1/attractions/2661515?locale=en-us&domain=ticketmaster.com"
               },
               "venue":{  
                  "href":"/discovery/v1/venues/229428?locale=en-us&domain=ticketmaster.com"
               }
            },
            "id":"1C0056DAC6666433",
            "_embedded":{  
               "venue":[  
                  {  
                     "name":"Punch Line Comedy Club - Sacramento",
                     "marketId":[  
                        41
                     ],
                     "country":{  
                        "countryCode":"US"
                     },
                     "state":{  
                        "stateCode":"CA"
                     },
                     "city":{  
                        "name":"Sacramento"
                     },
                     "location":{  
                        "latitude":"38.595101915",
                        "longitude":"-121.417434216"
                     },
                     "postalCode":"95825",
                     "address":{  
                        "line1":"2100 Arden Way",
                        "line2":"Sacramento, CA"
                     },
                     "timeZone":"America/Los_Angeles",
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/venues/229428?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"229428",
                     "type":"venue"
                  }
               ],
               "categories":[  
                  {  
                     "name":"Arts & Theater",
                     "level":1,
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/categories/10002?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"10002",
                     "type":"category"
                  },
                  {  
                     "name":"Comedy",
                     "level":2,
                     "_links":{  
                        "self":{  
                           "href":"/discovery/v1/categories/51?locale=en-us&domain=ticketmaster.com"
                        }
                     },
                     "id":"51",
                     "type":"category"
                  }
               ],
               "attractions":[  
                  {  
                     "url":"/Makeup-Mimosas-Madonna-Tribute-Show-tickets/artist/2661515",
                     "image":{  
                        "url":"/dbimages/358745a.jpg"
                     },


It generates the following error: TypeError: Cannot read property 'events' of undefined

Upvotes: 1

Views: 118

Answers (2)

Dupocas
Dupocas

Reputation: 21317

console.log(MainObject._embedded.events[0].locale) //en-us

Upvotes: 1

Andrew Xia
Andrew Xia

Reputation: 383

You're accessing events directly from data which is undefined.

You likely need to use:

result["_embedded"]["events"]

Upvotes: 0

Related Questions