recupero de una API extern la siguiente información
[{
"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
}]
Necesito convertir esta JSON a un tipo determinado.
En primer lugar el Date tiene que estar dentro de metadata --> timestamp-->value. No se como al crear el nuevo JSON omitir que aparezca en la primera posición y que pinte ese dato en el resto en el lugar indicado arriba.
Este es el resultado deseado:
{
"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"
}
}
}
}
Por otro lado cómo podía transformar esa fecha que contiene Date a un formato UNIX?.
Este es el codigo que estoy probando:
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));