Hello. I am querying an API (eia.gov) to get hourly electricity usage for certain entities (called "respondents") in the US. I got the HTTP request working fine, and a typical output is an array with 6 objects like this:
[{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"D","type-name":"Demand","value":27010,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"D","type-name":"Demand","value":26255,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"TI","type-name":"Total interchange","value":3691,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"NG","type-name":"Net generation","value":31689,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"TI","type-name":"Total interchange","value":-1293,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"NG","type-name":"Net generation","value":25717,"value-units":"megawatthours"}]
I then pass the above array through a function node that uses an array.map function:
const data = msg.payload.response.data;
// Extract the 4 desired fields using array.map
const extractedData = data.map(item => {
return {
period: item.period,
respondent: item.respondent,
type: item.type,
value: item.value
};
});
// Set the extracted data as the new payload
msg.payload = extractedData;
return msg;
Everything is fine except for the fact that the date (what is referred to as "period" representing a given hour of a given day) is being returned as a string...
and I want it to be DateTime format (or better, the epoch time format). I tried following this thread to use Date.parse()
and new Date()
in my function node above, but these gave me NaN
. I have to believe it is something simple that I have overlooked.