0

I want to parse the following JSON file, but I don't know how to parse an Object that is included by another Object. Here is my code:

var myRequest = new Request('test.json');

fetch(myRequest)
  .then(function(response) { return response.json(); })
  .then(function(data) {
          console.log(data. ??????????);

  });

JSON file:

{
    "products": {
        "29033669": {
            "anzahl": "11x",
            "img": "https://static.openfoodfacts.org/images/products/29033669/front_de.3.400.jpg",
            "name": "Zitronenteegetränk"
        },
        "4001686386613": {
            "anzahl": "1x",
            "img": "https://static.openfoodfacts.org/images/products/400/168/638/6613/front_de.17.400.jpg",
            "name": "Haribo Saft Goldbären"
        },
        "4008400207322": {
            "anzahl": "5x",
            "img": "https://static.openfoodfacts.org/images/products/400/840/020/7322/front_de.6.400.jpg",
            "name": "Kinder Schokolade"
        },
        "40518152": {
            "anzahl": "4x",
            "img": "https://static.openfoodfacts.org/images/products/40518152/front_en.3.400.jpg",
            "name": "Karamalz classic"
        },
        "676478232": {
            "anzahl": "1x",
            "img": "https://cdn4.iconfinder.com/data/icons/aami-web-internet/64/aami18-38-512.png",
            "name": "Bananen"
        },
        "7613035499768": {
            "anzahl": "4x",
            "img": "https://static.openfoodfacts.org/images/products/761/303/549/9768/front_de.6.400.jpg",
            "name": "Choclait Chips Classic"
        }
    }
}

Thank you very much for your help! Levi

levi.hrb
  • 51
  • 6

1 Answers1

0

First you have to know the type of data for this you use console.log(typeof(data)) If it is string then parse this string to object using const objData=JSON.parse(data) Now you can use data.products to access products.

If it is already in object type direct you can access using data.products.

sminmgr
  • 312
  • 3
  • 12