Ertser Commit der test-Version
This commit is contained in:
35
utilities/csv2json.js
Normal file
35
utilities/csv2json.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// convert influx csv output to JSON
|
||||
|
||||
export function csv2Json(str, delimiter = ",") {
|
||||
// slice from start of text to the first \n index
|
||||
// use split to create an array from string by delimiter
|
||||
const headers = str.slice(0, str.indexOf("\r\n")).split(delimiter).slice(3);
|
||||
let x = headers.findIndex((x) => (x === '_time') || (x === '_stop'))
|
||||
if (x != -1) {
|
||||
headers[x] = 'datetime'
|
||||
}
|
||||
// slice from \n index + 1 to the end of the text
|
||||
// use split to create an array of each csv value row
|
||||
const rows = str.slice(str.indexOf("\r\n") + 2).split("\r\n").slice(0,-2)
|
||||
|
||||
// Map the rows
|
||||
// split values from each row into an array
|
||||
// use headers.reduce to create an object
|
||||
// object properties derived from headers:values
|
||||
// the object passed as an element of the array
|
||||
const arr = rows.map(function (row) {
|
||||
const values = row.split(delimiter).slice(3);
|
||||
const el = headers.reduce(function (object, header, index) {
|
||||
if(header !== 'datetime') {
|
||||
object[header] = parseFloat(values[index]);
|
||||
} else {
|
||||
object[header] = values[index];
|
||||
}
|
||||
return object;
|
||||
}, {});
|
||||
return el;
|
||||
});
|
||||
|
||||
// return the array
|
||||
return arr;
|
||||
}
|
||||
Reference in New Issue
Block a user