0

I must be getting tired but I can't figure this thing out. I have the following code:

            fetch(url, options)
            .then(function(response) {
                return response.json();
            })
            .then(function(myJson) {
                setTimeout(function(){ 
                if (myJson == undefined) 
                {
                    console.log("fetch failed")
                } 
                else 
                {
                    //inspect the data that the WebAPI returned
                    console.log("myJson: ", myJson);
                }
                }, 3000);
            });

the variable url is populated with an API url that returns the following string of data (I got this by entering the string that I use to fetch directly into a browser):

{"products":[{"barcode_number":"5000159459228","barcode_type":"EAN","barcode_formats":"EAN 5000159459228","mpn":"","model":"","asin":"B01K4FU9K6","product_name":"Twix (50g x 6)","title":"","category":"Toys & Games > Toys > Pretend Play > Pretend Shopping & Grocery","manufacturer":"Mars","brand":"","label":"Mars","author":"","publisher":"Mars","artist":"","actor":"","director":"","studio":"","genre":"","audience_rating":"","ingredients":"","nutrition_facts":"","color":"","format":"","package_quantity":"","size":"","length":"","width":"","height":"","weight":"","release_date":"","description":"Milk chocolate covered caramel and biscuit.","features":[],"images":["https://images.barcodelookup.com/4488/44880860-1.jpg"],"stores":[{"store_name":"Rakuten Deutschland GmbH","store_price":"23.57","product_url":"https://www.rakuten.de/produkt/twix-32-riegel-grosspack-2179063860","currency_code":"EUR","currency_symbol":"€"}],"reviews":[]}]}

When I look at the console to see the log, this is what I see:

enter image description here

For some reason, I can not figure out how to reference a specific element in the myJson object.

How would I reference the barcode_formats field?

Thank you.

Jonathan Small
  • 1,027
  • 3
  • 18
  • 40
  • 3
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). It’s `myJson.products[0].barcode_formats`. – Sebastian Simon Aug 12 '18 at 17:36
  • Why are you using `setTimeout`? – Sebastian Simon Aug 12 '18 at 17:37
  • @Xufox - left over from a previous version of my application. I haven't removed it yet. Thank you so much for the format. I must be getting old! – Jonathan Small Aug 12 '18 at 17:53

0 Answers0