0

I need to convert a CSV file to a JSON output, I'm using the csvtojason library from https://www.npmjs.com/package/csvtojson, csv File:

mode,device,stb,data,data1,data2,data3,data4
modelo1,device1,stb1,data1,data2,data3,data4
modelo1,device2,stb1,data1,data2,data3,data4
modelo2,device1,stb1,data1,data2,data3,data4
modelo2,device1,stb2,data1,data2,data3,data4
modelo2,device2,stb1,data1,data2,data3,data4
modelo2,device2,stb2,data1,data2,data3,data4
modelo2,device2,stb3,data1,data2,data3,data4

code used to convert the json

csv = require('csvtojson')
csvFilePath = './CSVFile.csv'
  csv()
    .fromFile(csvFilePath)
    .then((jsonObj) => {
      console.log(jsonObj)
      res.send(jsonObj);
    })

But The output is far away from what I expected:

[ { mode: 'modelo1',
    device: 'device1',
    stb: 'stb1',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo1',
    device: 'device2',
    stb: 'stb1',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo2',
    device: 'device1',
    stb: 'stb1',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo2',
    device: 'device1',
    stb: 'stb2',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo2',
    device: 'device2',
    stb: 'stb1',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo2',
    device: 'device2',
    stb: 'stb2',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' },
  { mode: 'modelo2',
    device: 'device2',
    stb: 'stb3',
    data: 'data1',
    data1: 'data2',
    data2: 'data3',
    data3: 'data4' } ]

I'm expecting to get something like this:

[{
    modelo1 : {
        device1: {
            stb1:[data:'data1',data2:'data2',data3:'data3',data4:'data4']
        }
        device2: {
            stb1:[data:'data1',data2:'data2',data3:'data3',data4:'data4']
        }
    }
    modelo2 : {
        device1: {
            stb1: [data:'data1',data2:'data2',data3:'data3',data4:'data4']
            stb2: [data:'data1',data2:'data2',data3:'data3',data4:'data4']
        }
        device2: {
            stb1: [data:'data1',data2:'data2',data3:'data3',data4:'data4']
            stb2: [data:'data1',data2:'data2',data3:'data3',data4:'data4']
            stb3: [data:'data1',data2:'data2',data3:'data3',data4:'data4']
        }
    }
}]

Any idea is there is an easy way to do it?

  • Your expected output is invalid JSON. An array in JSON is written as e.g. "stb1": [ "data1", "data2", "data3", "data4" ]. You also lack commas between objects. Note that the JSON that you do get is actually easier to query than the JSON that you propose, as the keys are derived from the column headings rather than from the table data. – Kusalananda Jun 19 at 16:41
  • It was just an sample in how I would like it, let me fix it to the exact format – Felipe Tank Jun 19 at 16:44
  • Do you need to do in javascript, or is it ok a commandline tool? – aborruso Jun 20 at 6:21
  • Any way that I can solve it is ok – Felipe Tank Jun 21 at 9:20
0

I found an answer, using D3 libary is very easy

https://d3js.org/d3-collection.v1.min.js

on my .js file:

            var groupedJson = d3.nest()
                .key(function (d) { return d.model; })
                .key(function (d) { return d.device; })
                .key(function (d) { return d.stb; })
                .entries(json);
            console.log(groupedJson);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.