This is my CSV im starting with. I am trying to make a JSON file. The JSON file has an array for the Phases. An example bwlo can be seen with what im trying to achieve. I also inlclude the code im using and what the output for that is.
"name","id","code1","codeo","newfile","date","phases.id","phases.season","phases.num","phases.phatitle","phases.gcode","phases.ocode","phases.examples.scode","phases.examples.classty","phases.examples.durr","phases.examples.internalid"
"Person1",1756,,,true,"2020-05-25",-1,0,1,"winter",,,6789,"dc1",6000,"dfgdfgfdg213" ,,,,,,-1,0,4,"fall",,,6789,"dc1",7200,"dsgdgdfg1231" ,,,,,,-1,0,5,"summer",,,6789,"dc1",7800,"dsfsdffs1223"
I am trying to get a Nested JSON, so the end result is this, where there is an array for phases
{
"name": "Person1",
"id": 1756,
"newfile": true,
"date": "2020-05-25",
"phases": [{
"id": -1,
"season": 0,
"num": 1,
"phatitle": "winter",
"examples": {
"scode": 6789,
"classty": "dc1",
"durr": 6000,
"internalid": "dfgdfgfdg213"
}
},{
"id": -1,
"season": 0,
"num": 4,
"phatitle": "fall",
"examples": {
"scode": 6789,
"classty": "dc1",
"durr": 7200,
"internalid": "dsgdgdfg1231"
}
},{
"id": -1,
"season": 0,
"num": 5,
"phatitle": "summer",
"examples": {
"scode": 6789,
"classty": "dc1",
"durr": 7800,
"internalid": "dsfsdffs1223"
}
}
]
}
I am using this code in Node
const csvFilePath='./response.csv'
fs = require('fs')
const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
console.log(jsonObj);
fs.writeFile("response.json", JSON.stringify(jsonObj), function (err) {
if (err)return console.log(err);
return console.log('done')
})
})
I am receiving this output (The issue is that phases should be an array containing each object).
[{
"name": "Person1",
"id": "1756",
"code1": "",
"codeo": "",
"newfile": "true",
"date": "2020-05-25",
"phases": {
"id": "-1",
"season": "0",
"num": "1",
"phatitle": "winter",
"gcode": "",
"ocode": "",
"examples": {
"scode": "6789",
"classty": "dc1",
"durr": "6000",
"internalid": "dfgdfgfdg213"
}
}
}, {
"name": "",
"id": "",
"code1": "",
"codeo": "",
"newfile": "",
"date": "",
"phases": {
"id": "-1",
"season": "0",
"num": "4",
"phatitle": "fall",
"gcode": "",
"ocode": "",
"examples": {
"scode": "6789",
"classty": "dc1",
"durr": "7200",
"internalid": "dsgdgdfg1231"
}
}
}, {
"name": "",
"id": "",
"code1": "",
"codeo": "",
"newfile": "",
"date": "",
"phases": {
"id": "-1",
"season": "0",
"num": "5",
"phatitle": "summer",
"gcode": "",
"ocode": "",
"examples": {
"scode": "6789",
"classty": "dc1",
"durr": "7800",
"internalid": "dsfsdffs1223"
}
}
}]
It feels like every NPM I use I keep getting a very similar result where its breaking the JSON into a new records with no arrays for the actual phases portion.