question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Output from csvtojson not pure JSON

See original GitHub issue

I’m trying to use this tool to convert a csv to JSON. The output i’m getting is not exactly pure “JSON”.

What I tried:

var csvToJson = require('csvtojson');
const converter = csvToJson({
  ignoreEmpty: true,
});

csvToJson()
  .fromFile(csvFile)
    .on('json', function(jsonObj) {
      console.log(jsonObj);
     })
    .on('done', function() {
      console.log("done !");
    });

And the output was something like this:

{ 
  field1: '100051',
  Membership: '100051',
  gender: 'male',
}

And what i’m expecting:

{ 
  "field1": "100051",
  "Membership": "100051",
  "gender": "male",
}

So i’m expecting is quotes around the object keys, which is the standard JSON. How can i achieve that ?

Thanks in advance 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Keyangcommented, Jul 5, 2017

It is because console.log function you used does not print the quotes – you can try with other JSON Object in javascript and console.log will never print the quotes.

0reactions
jfr3000commented, Aug 7, 2018

I am having the same problem. The output from csvtojson() cannot be parsed as JSON. The thing about console.log is also not quite right: image

It seems like maybe you are equating Javascript objects with JSON? In any case, this:

csvtojson().fromString('name, age\njane,doe').then(result => JSON.parse(result))

will throw an error Unexpected token o in JSON at position 1, proving that the result is not in fact JSON.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nodejs - csvtojson not giving proper json keys in output
I have written below code to convert my csv file but it is not converting into json properly. The keys in json do...
Read more >
csvtojson
A tool concentrating on converting csv data to JSON with customised parser supporting. Latest version: 2.0.10, last published: 3 years ago.
Read more >
How to Convert CSV to JSON file having Comma ...
A CSV is a comma-separated values file with .csv extension, which allows data to be saved in a tabular format. Here is a...
Read more >
Convert CSV to JSON in a JavaScript App | by John Au-Yeung
Convert to CSV Lines. We can also get the raw CSV row one row at a time with the subscribe method and the...
Read more >
Top 10 CSV JavaScript Parsing Libraries 2022
Processing CSV to JSON - Top 10 CSV JavaScript Parsing Libraries 2022 ... We cannot easily filter it in its raw form or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found