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?