Retrieve and process various arrays from a .json document

I have been keeping track of all the movies I watch by storing my data in movies.json. Within movies.json, there are multiple arrays starting with "MarvelMovies" containing 2 movies.

Following that is "ComedyMovies" with another 4 movies listed.

While I am able to console.log all the arrays, I am still working on how to individually display each array (specifically log the MarvelMovies and ComedyMovies separately).

movies.json:

{
    "MarvelMovies": [{
            "Title": "The Avengers",
            "Year": "2012",
            "Poster": "https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
            "Ratings": [{
                "Source": "Internet Movie Database",
                "Value": "8.0/10"
            }, {
                "Source": "Rotten Tomatoes",
                "Value": "91%"
            }
            ...

my js fetch code:

//DOM element
let movieSlider = document.querySelector(".mainSlider"); //Grid for movies
let movieTitle = document.querySelector(".mainSlider__title"); //genre title
fetch('./media/json/movies.json')
.then(response => response.json())
.then((movies) => {
    console.log(movies)
//   console.log(response);
//   });
// .then(movie => {
    for (let i = 0; i < movies.length; i++) {
        const element = movies[i];
        movieSlider.innerHTML += `
        <div class="mainSlider__item" style='background:url("${element.Poster}"); background-size:cover;'>
        <div class="mainSlider__item-playButton">
            <div class="mainSlider__item-title">${element.Title}</div>
            <div class="mainSlider__item-info">${element.Year}</div>
            <div class="mainSlider__item-desc">${element.Runtime}</div>
        </div>
    </div>
        `


    }
});

Answer №1

In case you are unfamiliar with the property names and do not require them (as they appear to be categories), you can utilize the Object.values and Array.flat functions to extract an array consisting solely of movies:

const jsonData = `your json data`
const movieList = Object.values(jsonData).flat();
console.log(movieList);

Answer №2

json_data = `your json data`
movies_list = list(json_data.values())
print(movies_list)

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

What is the best way to send a populated custom class from JavaScript to a .NET MVC app using AJAX?

I am working with a .NET class in C# called BusinessModel: public class BusinessModel { public string BlobName { get; set; } public string NewName { get; set; } } In my MVC Ajax Controller, I have an action called DoBusiness: [HttpPost] public A ...

Error: Module not located in Custom NPM UI Library catalog

I have recently developed an NPM package to store all of my UI components that I have created over the past few years. After uploading the package onto NPM, I encountered an issue when trying to use a button in another project. The error message "Module no ...

What is the best way to tally the elements of a JavaScript array and produce a desired output format

I have an array that looks like this: ["5763.34", "5500.00", "5541.67", "5541.67"] I am looking to count similar values in the array and produce an output as follows: (1 * 5763.34) + (1 * 5500.00) + (2 * 5541.67) Does anyone have any ideas on how to ac ...

Issues with Collision Detection between Canvas Rectangles and Balls

I am developing an HTML5 canvas game and encountering an issue with collision detection. The problem occurs when the ball collides with any platform - the ball's gravity should change to -1 and move upwards at the same velocity as the platforms. Howev ...

Is there a way to integrate a MySQL database with parcel-bundler in a Node.js environment, or is there a simpler method to achieve this database integration with parcel-bundler?

Node.js and parcel-bundler are new to me, but I've managed to create a server.js file that connects to the database without any issues. Server.js const express = require('express'); const mysql = require('mysql'); //Establish con ...

Launch a fresh tab using Chrome's extensions feature

Currently, I am in the process of developing a Google extension that will search for selected text on Google when the user clicks "ctrl+alt+x". Here is the mainfest for the extension: { "name": "A jQuery Chrome extension", "version": "0.1", "descri ...

Retrieve an XML file using JavaScript and store it within a variable

I am currently developing a PhoneGap Application utilizing jQuery Mobile. One of the requirements is to access an xml file stored on a remote server (accessible through a web server like http://www.example.com/myXmlFile.xml). My goal is to extract the cont ...

The duplicate code is failing to display any output

Welcome to my first question here! Please excuse any rookie mistakes. I am currently working on a specialized calculator using HTML, JS (with jQuery), and CSS. This calculator is designed to handle multiple inputs and perform various calculations on a sing ...

The req.file Buffer object is not defined when using express.js

Implementing file upload functionality in frontend using React.js. const handleUpload = (e) => { setFormvalue({ ...formvalue, recevierImages: e.target.files[0] }); }; const submitData = () => { console.log(formvalue); dispatch(create ...

discord.py with a unique prefix, using a custom JSON configuration file

After going through countless tutorials and forums, all providing the same information about creating a custom prefix for a Discord bot, I am still unable to get it to work. Even copying and pasting the code and making necessary changes did not yield any r ...

Mongoose currency does not display fractional values

Struggling to implement a Schema with a 'price' field using Mongoose currency by following the guidance in the documentation. However, the output is displaying prices without two decimals (e.g., 499 instead of 4.99). Surprisingly, when I use cons ...

Chunk error ECONNREFUSED trigger

Encountered an issue while running through grunt. Getting a proxy error: Econnrefused when trying to run grunt serve. After running --verbose, it seems like the request is being blocked. I suspect it may be due to my organization's network setup, bu ...

Stopping a jQuery AJAX request when the user switches to a different page

A method has been defined for sending a jQuery AJAX request as shown below: sendAjaxRequest(URL, data) { return $.ajax({ type : 'POST', url : URL, crossDomain : true, data : JSON.stringif ...

Ways to ensure a button is ready to be clicked

For my beginner chrome extension project, I am facing a specific situation that I need help with. Imagine a scenario where I have a website with a search button. When the button is clicked, two possibilities can arise: A search result appears with a butt ...

Issue with Three.js: The mouse hover effect does not revert back to the previous color

Currently, I am working on creating a pattern using Three.js. The goal is to change the color of a face to gray when the user hovers over it with the mouse, and then revert it back to its original light blue color when the mouse moves away. Unfortunately, ...

Is there a more efficient method for iterating through this object?

Working with JSON and JS var data = { "countries": { "europe" : [{name: "England", abbr: "en"}, {name: "Spain", abbr: "es"}], "americas" : [{name: "United States"}], "asia" : [{name: "China"}] } }; JavaScript Loop for (k in data) { fo ...

Sending state properties to components within a route

In my React structure, I have the following setup: <Provider store={ store }> <Router> <Switch> <Route path="/how-to" component={ Help } /> <Route path="/start" c ...

Creating Event Handlers for corresponding elements in HTML with the help of JQuery and JavaScript

Struggling with HTML and debugging an issue in my ASP.NET Core App. The problem lies in a CSHTML view that functions as a timeclock system for tracking user input against job numbers. The current Index.cshtml is operational, verifying JobNumbers against t ...

What is the best way to eliminate a CSS style from a div?

I have implemented jQuery Autosize to automatically adjust the height of textarea elements. It works perfectly when I focus on the textarea element. However, when I blur out of the textarea, I want to reset the height to its default value. I am unsure of ...

Angular js: Understanding the use of "this" within the ng-if function

Is there anyone who can assist me with the following question: How do I access the "this" element within the ng-if (in my example, the classname is "class_to_obtain" of the span element)? http://plnkr.co/edit/0s7PCWN2fJ8sJpFSJssV HTML ...