Add additional keys to a JSON collection

I come across a situation with an array of JSON data retrieved by Zabbix as strings, where each object includes keys "startTime" and "endTime" in the format of "DD-MM-YYYY HH:mm:ss".

I am attempting to use Javascript to introduce a new key in each object within the array. The new key, named "execTime," should store the difference in seconds between "endTime" and "startTime."

Sample Input JSON:

[
    {
    "endTime": "02-02-2024 10:10:20",
    "startTime": "02-02-2024 10:10:10",
    },
    {
    (...)
    }
]

Expected output JSON:

[
    {
    "endTime": "02-02-2024 10:10:20",
    "startTime": "02-02-2024 10:10:10",
    "execTime": 10
    },
    {
    (...)
    }
]

To achieve this, I have been experimenting with the following script.

function (value) {
    const jsonValue = JSON.parse(value);

    for (var i = 0; i < jsonValue.length; i++) {
      const startDate = new Date(jsonValue[i].startTime);
      const endDate = new Date(jsonValue[i].endTime);

      const execTime = ( endDate.getTime() - startDate.getTime() ) / 1000;
      if ( execTime < 0 ) { var execTime = 0; }
        jsonValue[i].execTime = execTime;
    };

    return jsonValue;
}

However, the issue arises as the return value turns out to be "[object Object],[object Object],[object Object], (...)" instead.

[object Object],[object Object],[object Object],(...)

Any insights on this puzzle?

Answer №1

transform jsonValue into a string using JSON.stringify();

// Revert the output to a JSON-formatted text

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

Ajax operates by fetching data from a database without requiring large files to be retrieved

Every time I try to retrieve a file larger than 0.5MB from the SQL database using AJAX, an error occurs. What am I doing wrong here? $(document).ready(function () { loadFileData(); }); function loadFileData() { $.ajax({ type: "GET", ...

What is the best way to handle texture loading delays in Three.js when using a JSON model?

Currently, I have successfully loaded a JSON model based on AlteredQualia's skinning example. However, I am looking to hide the model until it is fully loaded. In the provided example, the models are displayed before their texture resources finish loa ...

PHP is failing to display line breaks when retrieving data from JSON sources

I have a JSON feed that is decoded by PHP using json_decode and then echoed onto my HTML page. The issue I am facing is that the feed contains \n characters, but when PHP echoes them out, they are not recognized as new lines or line breaks in HTML. In ...

Overriding the w-4xl with sm:text-2xl in Tailwind CSS

Struggling to achieve responsive design on my Pages, especially with changing text size when the screen is small. I've been following all the correct steps and maintaining the right order. However, whenever I set 'sm', it seems to override a ...

These specific resources don't have a cache expiration set. Wondering how to properly set a cache expiration for a JavaScript file

I am working on a web application that utilizes external JavaScript files. Upon running Google's page speed tool, I realized that several resources are missing cache expiration settings, including images and CSS files. Can you provide examples of how ...

Angular 5 combined with the dynamic capabilities of Popper.JS

Launching a training project that requires Popper.Js in its features has been quite challenging. Despite following Bootstrap's documentation on how to implement Popper.Js, I have encountered difficulties. As per Bootstrap's docs, I tried initiat ...

A quick guide on updating table values in Laravel framework without the use of forms or JavaScript

Currently, I am immersed in my project where I need to fetch data from a table named 'Clients' in a database and exhibit it. Initially, this was quite straightforward. However, I now wish to modify certain information in the table. The challenge ...

Utilizing the power of JDBC in conjunction with Elasticsearch and the Json data type in Post

Database Version: Postgresql 9.3.2 Elasticsearch Version: 0.90 Elasticsearch River JDBC Version: 2.2.2 Postgresql JDBC Version: 9.3-1100 JDBC 41 I am attempting to transfer a postgresql Json data type column into elasticsearch using the elasticsearch ...

Is there an optimal method for passing an associative array through the map/reduce function in MongoDB?

Below are the functions I have written: map: function () { // initialize KEY // initialize INDEX (0..65536) // initialize VALUE var arr = []; arr[INDEX] = { val: VALUE, count: 1 }; emit(KEY, { arr: arr }); } reduce: function (key, values ...

Identify when the input loses focus in a link directive

I have a query regarding an input that is using a link directive. Specifically, I am wondering if it's feasible to identify when the input loses focus within the link function of the directive. Below is the directive in question: appDrct.directive(& ...

Utilizing setColumns() function within Google Charts for JSON data tables

Is there a feature in the Google Charts API that functions similar to setColumns() for working with JSON data? I'm aiming to reduce PHP calls by consolidating my data into a single array and then specifying which columns Google Charts should utilize. ...

Mixing has() and gt() for great results

Having trouble combining has() with :gt in jQuery 1.4.2. I want to select any <ul> that has more than 3 <li>s, but my attempted solution is not working: $(document).ready(function(){ $("ul.collapse:has(li:gt(2))") .each( function() { ...

The "truth" parameter within the xmlHttpRequest .open() function

After referencing MDN, it mentioned that by default, the JavaScript function will keep running until the server response is received. This is essentially what AJAX is all about - Asynchronous JavaScript and XML. Even though I have some experience with A ...

Traffic signal color transitions without the use of a button

My objective is to have the following code run automatically without the need for user input. It should also execute in a loop instead of running through the sequence only once. I attempted to modify the code, but it either failed to work or altered the sh ...

Arranging array by value in Node.js

Currently, I am learning about node and facing a challenge with ordering an array in the following format: "lng" : [{ "ES" : 5, "EN" : 3, "IT" : 4 }] This array is part of a query result in MongoDB. My goal is to sort the arra ...

What is the best way to transform a class containing a map to and from a JSON string in JavaScript?

Recently encountered a challenge at work. Here is an example of a class I have: class A { map1: Map<number, number> = new Map(); map2: Map<string, string> = new Map(); // a bunch of other data age: number = 0; ... } The goa ...

Creating SQL server tables from JSON automatically in C#

Users are sending me JSON Form Data. Here is an example of the data: { "Name":"Mike", "Age":25, "Gender":"Male", "Skills":{ ".Net":true, "Mule":"" } } I need to save this data in a table on SQL Server, but there isn't ...

Ways to trigger the upgradeneeded event in IndexedDB without upgrading the version

Can you help me understand the "upgradeneeded" event? I want to be able to check the database every time a user reloads the page. Is there a way to trigger this without actually upgrading the version of the indexedDB? request.addEventListener('upgrad ...

The toggle function for displaying/hiding a table is not functioning properly when linked to the dropdown selection

My console is not showing any errors, but when I try to click on an element in the drop-down menu, a table should appear or disappear. Unfortunately, this functionality isn't working as expected. You can view it in action here: (Code snippet ...

Adding a javascript file to a Vue component

I am currently working on a project that involves using Laravel and Vue.js. In order to incorporate an image editing package called "photojshop" into my Vue component, I tried the following approach: <template> <img src=".." id="myImg" > </ ...