I wanna convert CSV to JSON correct data type
csv file 2nd row is data type. data has over 300 properties example data:
| Name | DMG | HP | Human |
|---|---|---|---|
| string | number | number | boolean |
| knight | 100 | 500 | true |
| archer | 50 | 200 | true |
| dog | - | - | - |
if string empty return null
if number empty return 0
if boolean empty return false
my node.js code:
const fs = require('fs')
const papa = require("papaparse")
const results = [];
const options = { header: true, dynamicTyping: true };
fs.createReadStream("characters.csv")
.pipe(papa.parse(papa.NODE_STREAM_INPUT, options))
.on("data", (data) => {
results.push(data);
}).on("end", () => {
console.log(results)
})
output I expecting:
[
{
"Name": "knight",
"DMG": 100,
"HP": 500,
"Human": true,
},
{
"Name": "archer",
"DMG": 50,
"HP": 200,
"Human": true,
},
{
"Name": "dog",
"DMG": 0,
"HP": 0,
"Human": false,
},
]