Loop through JSON data using JavaScript

Recently, I came across this JSON data:

{"index_name":"grafana1","begin":"1970-01-01T00:00:00.000Z","end":"1970-01-01T00:00:00.000Z","calculated_at":"2022-06-01T00:00:04.099Z","took_ms":0},{"index_name":"grafana2","begin":"1970-01-01T00:00:00.000Z","end":"1970-01-01T00:00:00.000Z","calculated_at":"2022-06-02T00:00:05.099Z","took_ms":0}

I have a website that uses vanilla JavaScript and I want to only showcase the values of "index_name" and "calculated_at" from this JSON string.

To achieve this, I am seeking a method to iterate through the entire string and display only the values associated with these two keys in each entry.

The final output on my website should resemble the following format:

grafana1
2022-06-01T00:00:04.099Z

grafana2
2022-06-02T00:00:05.099Z

Answer №1

function filterData(key, value) {
  console.log(typeof value);
  if (key !== 'name' && key !== 'date_updated') {
    return undefined;
  }
  return value;
}

let filteredData = JSON.stringify(dataObj, filterData);
console.log(filteredData);

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

"Is there a way to verify the existence of checkbox array values within a database using PHP Laravel

In order to display checkboxes for each country that exists in the database, I need to verify if the countries in the database match with those in the list. Since the values are retrieved directly from the database and can't be set as input values due ...

Utilizing jQuery to explore a JSON object from Google Books

I successfully retrieved a list of books using the Google Books API. I stored the JSON object in localStorage and then converted it back into an object: var books = JSON.parse(localStorage.books); Now, I am looking to retrieve information for a specific ...

Issue with pop up window causing button click event to malfunction

I am facing an issue where the button click event works fine outside of a pop-up window but does not fire when inside the pop-up window. In my HTML code, the part that calls the button event outside of the pop-up window works perfectly, but inside the pop- ...

The scheme is not valid; the connection string should ideally begin with either "mongodb:" or "mongodb+srv://"

After deploying my API, I encountered an issue when trying to send a GET request to the database. The error message displayed was Runtime.UnhandledPromiseRejection with the additional note saying "Invalid scheme, expected connection string to start with &a ...

Exporting a function to another class using the default export is a common practice

I'm working with two classes, Orders.js and api.js. In api.js, I need to invoke a function called ShowAlertWithDelay. However, due to the existence of a default export named mapStateToProps, I am unable to declare another export. Below is the code sni ...

Interactive sunburst chart with dynamic sizing for child nodes in JSON data

I utilized the following code: root = d3.hierarchy(root); root.sum(d => d.size); However, my JSON does not include the size value for children nodes. How can I create a proper D3 Zoomable sunburst visualization? Here is the code snippet: const width ...

A step-by-step guide on transferring data from a dynamic user interface to a server using React Native

In the following code snippet, I am dynamically retrieving an array value to generate a user interface. The UI will vary based on the dynamic nature of this value. However, I'm facing confusion regarding how to upload all the data to the server upon ...

Struggling to retrieve a JSON array value from a JSON object in Java for Android Development?

After receiving a JSON response from an API containing color information, I encountered an issue accessing the html_code value from the background_colors array within the info JSON object. My attempt to retrieve this data using code resulted in printing o ...

The element type provided is not valid: it should be a string for built-in components or a class/function for composite components. However, an object was received instead. - React Native

After conducting extensive research, I have been unable to find a solution as to why this issue persists. Can anyone shed some light on what the error might be referring to? Error: Element type is invalid: expected a string (for built-in components) or a c ...

In Pure JavaScript, an HTML element is added every time the click event is triggered

Hey everyone, I'm facing a small issue and I could use some help fixing it. I need to implement an onclick event that adds HTML code every time it's clicked. I am hesitant to use innerHTML due to its potential risks. Here is the code snippet: bu ...

How to show JSON formatted data in a Blade template

Showcasing json data in a blade template I have a database table where I store various types of data including json, and everything works well until I try to display that json data in a blade template. Here is an example of my json data: [![enter image des ...

Click the button to activate the JavaScript that will display the list

Could you please use the 'for' loop to display the list when clicking the button? Only the active button should work, while the others remain inactive. The current solution is not satisfactory. <body> <div> <ul ...

What is preventing me from accessing a JSON file using another program while my Processing sketch is running?

Currently, I'm using the saveJSONObject command in Processing to write data to a JSON file. The issue arises when I try to access that same JSON file with a different program (MAX/MSP) while my sketch is still running. For some reason, MAX is unable t ...

Modifying the attribute of an element inside an array

Presented below is an object: { "_id" : ObjectId("5a8d83d5d5048f1c9ae877a8"), "websites" : [ "", "", "" ], "keys" : [ { "_id" : ObjectId("5a8d83d5d5048f1c9ae877af"), "name ...

Storing Images in MongoDB with the MEAN Stack: A Guide using Node.js, Express.js, and Angular 6

I am currently working on developing a MEAN Shop Application, and I have encountered an error while attempting to store the product image in MongoDB. typeError: cannot read the property 'productimage' of undefined Below is the route function fo ...

Exporting dependencies' dependencies in Angular 2 through multi-providers

Utilizing a multi-provider is the next step I'm considering in order to export both dependencies of my dependency along with itself, allowing for them to be injected into a component simultaneously. For example, when setting up a component: import { ...

Issue with Material-ui Autocomplete: onChange event not firing when value is updated in onHighlightChange

I have been customizing Material UI's Autocomplete by adding a feature that allows users to move options to the input using keyboard events (Arrow keys up and down). Then, the user should be able to select an option by pressing the ENTER key. I am fa ...

Clicking on a flex card will trigger the opening of a new page where the parsed data will be displayed in a stylish manner using JavaScript

Currently, I am attempting to showcase the data retrieved from the following URL: . My objective is to format the parsed data with a specific style. However, I have encountered difficulties accomplishing this using `window.open()`. Any assistance in resolv ...

The string becomes tainted following the transcoding process

In order to describe the issue in detail, I have provided a simplified version of the following code: #include <bits/stdc++.h> #include <iostream> #include <regex> #include <string> #include <string> #include <Windows.h> ...

Create an array populated with unclosed HTML tags that need to be rendered

I'm trying to display a simple list, but it seems like React is having trouble rendering unclosed HTML elements that are pushed onto the list. This results in an error: ReferenceError: el is not defined If I use single quotes (') bookingStat ...