Struggling to access specific data within a JSON object? Wondering how to extract and display data from a JSON object in VUE?

Despite my extensive searching on Stack and the internet, I have not been able to find a solution to my problem.

Currently, I am attempting to retrieve data from a JSON file located in the Vue src folder. The file contains three arrays with names that include dots. While I could manually edit the array names since the file is local, I want Vue to dynamically retrieve this information from a web server in the future, so I don't want to make any changes now.

I only need to extract specific bits of information from the file but I am struggling to isolate that data.

The following is the JSON data:

 {
  "staging_nr_front_33.333.33.333 ": {
   "date": "2018-09-10 13:05:02",
  "webserver": "running",
  "memory": "79MB of 527MB available",
  "token_status": "present",
  "space": "5.3G of 16G used (34%) 11G available"
 },
 "staging_roll_444.444.444.44 ": {
  "date": "2018-09-10 13:02:44",
  "webserver": "running",
  "memory": "391MB of 993MB available",
  "token_status": "present",
  "space": "4.3G of 39G used (12%) 32G available"
},
"staging_nr_cont_55.555.555.555 ": {
"date": "2018-09-10 13:05:02",
"webserver": "running",
"memory": "94MB of 7302MB available",
"token_status": "present",
"space": "3.9G of 7.8G used (50%) 3.9G available"
}
}

Below is my Vue Script section:

 import roomIndex from '../serv.json';

  export default {
   name: 'HelloWorld',
   data() {
       return {
           roomIndex
       }
   },

And here is how I'm trying to access it in my template section:

<p v-for="dt in roomIndex">{{ dt.date }} </p>

Currently, using this format allows me to successfully retrieve all the dates in the JSON file. However, when I try to single out just the first date by writing:

  <p v-for="dt in roomIndex">{{dt[0].date }}</p>

I receive an error: Error in render: "TypeError: Cannot read property 'date' of undefined"

I have attempted various approaches such as enclosing the array name in brackets like:

 <p v-for="dt in roomIndex">{{ dt.[staging_nr_front_33.333.33.333].date }}</p>

Unfortunately, that did not work either.

How can I specifically retrieve the date object from the first array?

As a newcomer to Vue and JSON, I appreciate your patience if this is a simple fix or if my code seems unusual...

Thank you for your assistance!!!!

Answer â„–1

After reviewing another comment, I realized that the issue was due to an object being used instead of an array, which explained why it wasn't functioning as expected. In order to display both the date and key of each server (in different parts of the page), I devised the following solution:

<div  v-for="(value, key, index) in roomIndex">
      <div class="box>
        <h2>{{ key }}</h2>
        <h1>Server Status</h1>
        <div  class="circle" :class="value.webserver"></div>
           <h3>Date</h3>
        </div>
       </div>
</div>

Implementing this code resulted in a box displaying the server name, date, and a colored circle to indicate the server's status for each server. This resolved my issues effectively.

I appreciate all the assistance provided!

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

Verify the validation of the text box

Checking a textbox control for validation is necessary. Validation Criteria: Value should be between 0 and 1000, with up to 2 decimal places (e.g. 1.00, 85.23, 1000.00). Once 2 decimal points are used, users should not be able to enter additional ze ...

Draggable resizing of the Accordion component in React.js using Material-UI

In the visual representation, there are two Accordions—one positioned on the left (30%) and the other on the right (70%). Upon clicking the button, the right accordion disappears, while the one on the left expands to cover the full width (100%). A featu ...

Leveraging global variables within Vuex state management strategy

I have successfully added custom global variables into Vue by injecting them. Here is the code snippet: export default function (props, inject) { inject('models', { register(name) { const model = require(`@/models/${name}. ...

Interact with JSON data using CakePHP for posting and retrieving requests

Having difficulty posting and retrieving JSON data using CakePHP. In the view file, attempting to post data with code resembling the following: $.ajax({ type: "post", url: '/invoices/insertItem/', data: {'description': "th ...

Retrieve the date from 7 days ago and display it in the format "2015-06-23" using JQuery/JavaScript

Looking to retrieve last week's date in the following format: "2015-06-23", as opposed to "2015-06-16". JavaScript: t = new Date(); // Tue Jun 23 2015 21:00:47 GMT-0700 (PDT) t.toISOString(); // "2015-06-24T04:00:47.955Z" The current date format i ...

Encountering an Issue with NPM Run Production in PostCSS

I keep encountering the same error whenever I attempt to execute 'npm run production'. The remainder of the error consists of a compilation of 'node_modules' packages where this issue is also present. ERROR in ./resources/assets/sass/ap ...

Issue with Kendo dropdown's optionLabel functionality malfunctioning

Check out the Kendo code snippet below for a dropdown control. I'm currently facing an issue where I am trying to display a "Please select" option in the dropdown. This code works perfectly fine for all other dropdowns except for this specific one. T ...

Clicking to Load Images - Angular

Implementing a feature to load sets of images on button click instead of loading all at once. Although lazy load plugins are available, I decided to experiment with this approach. Here's the logic: Start with a data array called 'Images' co ...

Styling multiple Higher Order Components (HoCs) using Material UI withStyles

When developing my application, I encountered an issue with using Higher Order Components (HoCs) and withStyles for styling. If I apply multiple HoCs to one component, the classes prop of the first HoC gets passed to the next one in the compose chain, caus ...

The Angular JS Factory fails to send data back to the controller

When I call the method getPopularMovies in my factory using the controller, it returns undefined. I'm not sure what mistake I've made here. Please help me figure it out. My Factory angular.module('ngMovies').factory('moviesFactor ...

An issue with the JSON format encountered in the D3 pack layout

I am completely new to d3 and have only been learning it for the past 3 days. I attempted to create a pack layout, but I encountered an issue where I couldn't call the translate (or transform) function based on the data in an external json file. The s ...

Does a React functional component continuously re-render if it contains a child component?

For the past few days, I've been facing a performance issue in a React app (specifically React Native). The core of the problem is this: Whenever a function component Parent has another function component as its Child, the Parent will consistently re ...

Dealing with a missing response in an Ajax Server-Side Call: The .done(function(data){..} function remains inactive

Let's say I make an Ajax server-side call using jQuery like this: $.ajax({ url: "/myapp/fetchUser?username=" + username, type : "get", dataType : "json", data : '' }).done(function(data) { co ...

Asynchronous data fetching with React Hook useEffect does not properly populate the tooltip in Material UI component

After using useEffect to fetch data, I encountered a problem in passing the data to my component. Here is some of my code: User Data Types (UPDATED) export interface IUser { display_name: string; id: string; images: Image[]; } expo ...

AngularJS - Import and save CSV files

I have set up a nodeJS program as the server and an AngularJS web application as the client. For generating CSV files, I am utilizing the "express-csv" library (https://www.npmjs.com/package/express-csv) Below is the code for the server side: Definition ...

Separate the elements with a delimiter

I am in the process of inserting various links into a division by iterating through a group of other elements. The script appears to be as follows $('.js-section').children().each(function() { var initial = $(this).data('initial'); ...

Transferring data from jQuery Ajax to PHP

I'm facing a challenge in retrieving a value back to PHP that I can manipulate and save to the database. It appears that using the GET method with jQuery AJAX is not yielding the desired results. Below is the PHP code snippet where I attempt to captur ...

Prop validation error: The property "title" must be a string with a value of **, but a number with a value of ** was provided

My Vue js code displays data from a JSON API in a modal. The issue is that the title should be dynamic, but it only works when clicked twice. On the first click, I get an error and the cancel button, which triggers the hidemodal() function, crashes. Can an ...

Move the location of the mouse click to a different spot

I recently received an unusual request for the app I'm developing. The requirement is to trigger a mouse click event 50 pixels to the right of the current cursor position when the user clicks. Is there a way to achieve this? ...

Arranging elements on a webpage using Javascript

Creating a Dynamic Div Layout with JavaScript I have successfully implemented functionality wherein clicking on + opens a div and clicking on - closes it. However, I am now faced with the challenge of handling multiple divs at runtime. How can this be ach ...