I retrieve from an extern API the following information
[{
"date": "2019-05-14 13:30:00",
"4_X_X_30_last": 6863,
"7_X_X_7_last": 6559,
"11_X_X_600_avg": 361,
"21_X_X_650_last": 36.906471,
"22_X_X_651_last": -2.209531,
"23_X_X_652_last": 139.1,
"24_X_X_659_last": 0.9,
"2000_001EC0953E41_X_33798_last": 6196,
"2001_001EC0953E41_X_33799_last": 9888,
"2004_001EC095ADBE_X_34049_avg": 379,
"2004_001EC095ADBE_X_34049_max": 384,
"2004_001EC095ADBE_X_34049_min": 375
}]
I need to convert this JSON to a certain type.
First of all the Date has to be inside metadata --> timestamp-->value. I don't know how, when creating the new JSON, to omit that it appears in the first position and to paint that data in the rest in the place indicated above.
This is the desired result:
{
"4_X_X_30_last": {
"type": "float",
"value": 6863,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"7_X_X_7_last": {
"type": "float",
"value": 6559,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"11_X_X_600_avg": {
"type": "float",
"value": 361,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"21_X_X_650_last": {
"type": "float",
"value": 36.906471,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"22_X_X_651_last": {
"type": "float",
"value": -2.209531,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"23_X_X_652_last": {
"type": "float",
"value": 139.1,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
},
"24_X_X_659_last": {
"type": "float",
"value": 0.9,
"metadata": {
"timestamp": {
"type": "String",
"value": "2019-05-14 13:30:00"
}
}
}
}
On the other hand, how could I transform that date that contains Date to a UNIX format?
This is the code I am testing:
var pruebas = [{ "date": "2019-05-14 13:30:00", "4_X_X_30_last": 6863, "7_X_X_7_last": 6559, "11_X_X_600_avg": 361, "21_X_X_650_last": 36.906471, "22_X_X_651_last": -2.209531, "23_X_X_652_last": 139.1, "24_X_X_659_last": 0.9, "2000_001EC0953E41_X_33798_last": 6196, "2001_001EC0953E41_X_33799_last": 9888, "2004_001EC095ADBE_X_34049_avg": 379, "2004_001EC095ADBE_X_34049_max": 384, "2004_001EC095ADBE_X_34049_min": 375 }];
var key = Object.keys(pruebas)[0];
var finalobj = {};
for (var e in pruebas[key]) {
entidad: finalobj[e] = {
type: "float",
value: parseFloat(pruebas[key][e]),
metadata: {
timestamp: {
type: "String",
value: pruebas[key][e]
}
}
};
}
console.log(JSON.stringify(finalobj,null,2));
With this function you can do the two things you raise in your question
You can verify the value if the date is correct in this url: https://www.epochconverter.com/