0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
TWF
  • 45
  • 6
  • Node, "by itself," probably cannot accomplish this entire job for you. *(If it can, I'd sure be interested to know!)* – Mike Robinson Feb 04 '21 at 00:02
  • Your going to have to code something that does it because what you want does not really make sense with a CSV. – epascarello Feb 04 '21 at 00:03
  • Thanks! for some reason https://json-csv.com/reverse works perfectly when trying to go back to a JSON from the CSV, however I am trying not to use outsite websites as much. – TWF Feb 04 '21 at 00:06
  • 1
    The people behind that site wrote code to parse column headings with delimiters into nested objects. You'll need to code something like that yourself. See https://stackoverflow.com/questions/8051975/access-object-child-properties-using-a-dot-notation-string – Barmar Feb 04 '21 at 00:13
  • what is a `bwlo` -> `An example bwlo can be...` doesnt pas the translator to my native language – Mister Jojo Feb 04 '21 at 00:17

0 Answers0