JavaScript is a powerful tool for reading JSON files

I'm trying to figure out how to parse a nested object in JSON using JavaScript. Here's the code I have so far:

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

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

  });

Here is the JSON file I am working with:

{
    "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"
        }
    }
}

Any help on this would be greatly appreciated! Levi

Answer №1

To determine the data type, start by using console.log(typeof(data)). If it is a string, you can parse it into an object with const objData=JSON.parse(data). Then, you can access the products with data.products.

If the data is already in object form, simply access the products using data.products.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to achieve horizontal auto-scrolling in an image gallery with jQuery?

Hey there, I'm currently working on an Image Gallery project. I have arranged thumbnails horizontally in a div below the main images. Take a look at this snapshot img. My goal is to have the thumbnails scroll along with the main pictures as the user ...

Change the x and y positions of several div elements as the mouse moves in JavaScript

I am aiming to create a webpage where multiple divs with text and other content move along the x and y axes of the mouse. The desired effect is similar to parallax scrolling, but I have found that existing parallax plugins are image-based and do not work w ...

Suggestions for displaying combination data in JSON format?

Currently, I am working on integrating an API that provides information on the number of users using a specific app. For instance, let's say I need to retrieve data indicating 10 individuals are exclusively using App1, 8 are solely using App2, 8 are ...

What is the reason behind React deciding to skip the final question, even though it has been accurately saved to the

A simple web application was developed to calculate risk scores by asking five questions, with additional points awarded if a checkbox is checked. Despite extensive hours spent debugging, a persistent bug remains: the final question does not appear on the ...

Assign a value to a variable within the $_POST array for validation purposes using Codeigniter

I am currently developing a WebSocket based application using Codeigniter. The data is being received as a JSON string (not via a POST Method) and I want to utilize Codeigniter's built-in form_validation method to validate this JSON data. Here are th ...

What causes the disparity between the output of my PHP dump and JSON?

When working on my project using Laravel Eloquent, I noticed that I get different object results after further coding. When I use PHP dump functions (var_dump(), dd(), dump()), the object includes unnecessary data. On the other hand, when I use json_encod ...

Generating parameters on the fly from an array using jQuery

After implementing a successful plugin on my website, I am now looking to enhance it further by defining state-specific styles dynamically. The current setup allows for passing parameters for specific states, as shown below: $('#map').usmap({ ...

An issue occurred with Meteor while attempting to load or refresh the page: A Tracker afterFlush function threw an exception that is undefined

Encountering an error resulting in a blank page, with the exception of the navbar, when refreshing or directly accessing the link in a browser. This issue arises consistently under these circumstances, whereas navigation through the templates works fine. I ...

Reviving jQuery Smooth Zoom Pan viewer following container resize

I am in the process of developing a responsive image viewer and have decided to incorporate the Smooth Zoom Pan plugin for enhanced pan/zoom functionality. Within my layout, I have two primary panels: the viewer container and a controls container that are ...

Problem with Material-UI Drawer

Is there a way to make this drawer stay fixed on the page like a sticker and remain active without moving when scrolling? I've tried using docked={false}, but it makes the whole page inactive except for the drawer. Any suggestions on how to solve this ...

What is the need for assigning a name to every object in the Firebase data structure?

Exploring a simple array example from Google "chats": { "one": { "title": "Historical Tech Pioneers", "lastMessage": "ghopper: Relay malfunction found. Cause: moth.", "timestamp": 1459361875666 }, "two": { ... }, "three" ...

Assign the filename to the corresponding label upon file upload

I've customized my file uploader input field by hiding it and using a styled label as the clickable element for uploading files. You can check out the implementation on this jsFiddle. To update the label text with the actual filename once a file is s ...

Tips for deleting key value pairs from a JSON file using Java

Can anyone provide me with guidance on how to manipulate the provided dummy JSON file using Java? The head object in the file contains various values and children that follow a similar structure. I am looking for a way to eliminate all keys where the val ...

What is the process for extracting all data members and fields from an object in Java?

Is there a way to retrieve all member functions and fields defined in an object in Java? I want the ability to pass any object and have it print out all of its methods and fields along with their values. For instance, how does a JSON par ...

Tips for selecting a specific input field in a ReactJS component with multiple inputs

I am currently developing a ReactJS application where I have implemented a component responsible for generating an HTML table. Within this component, I utilize Map to create rows using a child component. These rows contain multiple input fields that users ...

Most Effective Method for Directing Users to Mobile Website

How do you prefer to guide users to a custom mobile site, such as redirecting from mysite.com to m.mysite.com or mysite.com/m? I previously experimented with the Detect Mobile Browsers JS solution, which seemed promising. However, I noticed that it initia ...

Shifting a div from side to side

I don't have much experience with coding, so I was hoping for some assistance. My goal was to create a div that moves back and forth using toggleClass; $('div').on('click', function() { $(this).toggleClass('active') ...

Troubleshooting problem with Angular's ng-repeat directive in dealing with varying number of child objects

I am currently dealing with tree-structured data where the parent nodes can have an indefinite number of children, and those children can also have an indefinite number of children, creating a deeply nested structure. While I have successfully formatted th ...

What is the best way to incorporate auto refresh in a client-side application using vue.js?

Disclaimer: I have separated my client application (Vue.js) from the server side (DjangoRest). I am utilizing JWT for validating each request sent from the client to the server. Here is how it works - The client forwards user credentials to the server, an ...

Troubleshooting: Issue with AngularJS $location.path functionality

I have some queries related to the usage of $location.path(/helpmeunderstand). I have a method called login() and when the login credentials are successful, I want to redirect to $location.path(/login/:username). However, instead of displaying the username ...