0

First question here,

Im trying to get a JSON object from a csv file using csvtojson npm module.

Everything works except the JSON object that i get has object keys on it:

Expected result:

{
 {"key": "value",
  "other_key": "other_value"
 },
 {"key": "value",
  "other_key": "other_value"
 }
}

Obtained:

{
 1:{
   "key": "value",
   "other_key": "other_value
 },
 2:{
    "key": "value",
    "other_key": "other_value
 }
}

My code for creating the JSON object is as follows:

csv({delimiter:";" }).fromFile(csv_path+name_csv)

The csvfile is as follows:

TITLE;TITLE2;TITLE3;TITLE4;TITLE5
string;string;int;string;int
string;string;int;string;int

1 Answers1

0

Consider this implementation:

const csv = require('csvtojson')

var csvStr = `TITLE;TITLE2;TITLE3;TITLE4;TITLE5
string;string;int;string;int
string;string;int;string;int`

csv({
  delimiter:";"
})
.fromString(csvStr)
.then((csvRow)=>{
    console.log(csvRow)
})

It outputs an array of objects:

[ { TITLE: 'string', TITLE2: 'string', TITLE3: 'int', TITLE4: 'string', TITLE5: 'int' }, { TITLE: 'string', TITLE2: 'string', TITLE3: 'int', TITLE4: 'string', TITLE5: 'int' } ]

GrafiCode
  • 3,307
  • 3
  • 26
  • 31
  • 1
    Thanks! but i figured out what was it, and now i feel dumb for asking... The object I get is just as you said, but then I print it with console.log(JSON.stringify(jsonobj)) and is than method what puts the numbers there. Sorry for the inconvenience, and thanks for the answer! – Jorge Gómez Feb 03 '20 at 12:25