-1

I have some plain machine learnig data set in separate lines like this:

vhigh,vhigh,2,4,small,low,unacc

vhigh,vhigh,2,4,small,med,unacc

vhigh,vhigh,3,more,med,low,unacc

... 1700 more lines like above

I want to convert data into structure like this:

[
  {
    "buying": "vhigh",
    "maint": "vhigh",
    "doors": "2",
    "persons": "2",
    "lug_boot": "small",
    "safety": "low",
    "evaluation": "unacc"
  },
  {
    "buying": "vhigh",
    "maint": "vhigh",
    "doors": "2",
    "persons": "2",
    "lug_boot": "small",
    "safety": "med",
    "evaluation": "unacc"
  },
...

]

and save converted data into new file.json

#edit : Node.js runtime

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • The question lack context, are you in a browser ? Node runtime ? Other ? Dou you already have the dataset in a string variable ? Check other questions related to CSV parsing in JS: https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript – YannPl Oct 18 '21 at 11:44
  • 2
    What have you tried? Where are you stuck? Did you look into parsing CSV text,? What environment are you going to run this on? SO is not a code writing service where you can dump your requirements like this. – Cerbrus Oct 18 '21 at 11:44
  • Yes, I forgot about the runtime - its node.js I wanted to use the regex – Klaudiusz Florek Oct 18 '21 at 11:48

1 Answers1

1

I found the answer. Thank you for targeting the csv. It was enough to use the conversion tool here: https://csvjson.com/csv2json

After conversion: csv converter

  • Doesn't seem like a good option when you need to transform large chunk of data or format your data in the background. I would rather look for some [transform stream](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams) that would [read input line by line](https://nodejs.org/api/readline.html) and write formatted data into output. – Yevhen Horbunkov Oct 18 '21 at 11:59