from Hacker News

Show HN: Npm module that makes easy to convert JSON to CSV

by kauegimenes on 6/8/17, 7:24 PM with 63 comments

  • by billforsternz on 6/8/17, 8:40 PM

    Off topic rant I need to get off my chest. Yesterday I troubleshooted a bizarre problem. New versions of Excel don't necessarily understand the simplest CSV files. Reason: They forgot that the C in CSV stands for comma, and instead expect whatever character happens to be set as the 'delimiter' character in the Windows regional preferences. Here in New Zealand, it seems that character is semicolon instead of comma. For some reason. I think Excel was finished circa 2000 and now they focus mainly on making it worse.
  • by dc2 on 6/8/17, 7:53 PM

    For a <5Kb library, I recommend removing the underscore dependency. You should make this as lightweight as possible if you want a lot of adoption.
  • by TAForObvReasons on 6/8/17, 8:21 PM

    The most popular NPM module for JSON to CSV conversion is json2csv: https://www.npmjs.com/package/json2csv https://github.com/zemirco/json2csv
  • by codezero on 6/8/17, 7:45 PM

    Cool, I immediately wondered if you handled a lot of the tricky CSV things like quotes and internal commas, it looks like it does a good job.

    Comparing against Numbers (probably a bad comparison?) I am seeing one slightly different result:

    If I have a field that is "hehe"

    It's encoded as ""hehe""

    It looks like Numbers adds the enclosing quotes:

    """hehe"""

    I only see enclosing quotes if there's a comma:

    "hehe,hehe" -> """hehe,hehe"""

    Anyhow, I'm not sure what the "correct" thing to do here is, if there is one, just a heads up!

  • by danso on 6/8/17, 7:43 PM

    In the complex object example, what's the usecase for turning that nested object into a multi-column CSV? I guess the overarching question is, why turn a single object at all into a CSV? It's just as machine/human readable as the JSON version of it, and there's no particular benefit in using either structure as it's not data meant to be processed in bulk.

    The complex array usecase is where an opinionated-type of conversion tool is particularly needed, but I wonder why it behaves like this:

       name: 'Robert',
       lastname: 'Miller',
       family: null,
       location: [1231,3214,4214]
    
    
    lastname,name,family.type,family.name,nickname,location Miller,Robert,,,,1231,3214,4214

    Why not have `location_1, location_2, location_3`, instead of having a single location column? The latter implementation makes the data difficult to quickly use in a program (like a spreadsheet).

  • by kmike84 on 6/8/17, 11:53 PM

    From a Python developer point of view, design is a bit surprising - why isn't it created as two separate libraries:

    1) csv writer (and reader?) which takes care of all csv dialects crazyness; 2) a library which "flattens" nested objects/arrays?

    (1) is not opinionated (besides a few API choices), it just has to be correct; it doesn't make much sense to re-implement (1) everywhere.

    (2) can be more opinionated, it is easy to disagree with design choices, there is more room for personal preferences.

    For example, in Python there is CSV stdlib module, and for (2) there are libraries like https://github.com/scrapinghub/flatson. Why put both to the same library? Is it something ideomatic in node.js world, with a deeper reason to design libraries this way (e.g. download size), or is it just an oversight?

  • by BrandiATMuhkuh on 6/8/17, 11:26 PM

    Converting JSON to CSV is a usable/good way when analysing NoSQL data. However I'm trying try find since a while a data analysis/query tool for JSON data itself. I saw that mixpanel made a query tool/language called JQL (JavaScript query language) which seems nice. Also RQL (rethink query language) seems nice too. But I can't​ find a simple tool in which I can import my JSON data and query it. It should be similar to SQLite-browser. Simply import your CSV/JSON file, and query it.
  • by chid on 6/9/17, 4:01 AM

    There's a similar command in pandas called json_normalize (http://pandas.pydata.org/pandas-docs/stable/generated/pandas...)
  • by freezer333 on 6/8/17, 9:15 PM

    Nice! If you want to do something similar with Excel xlsx this works: https://www.npmjs.com/package/jsonexcel Not sure how it handles huge arrays though...
  • by madamelic on 6/8/17, 8:18 PM

    This is a new one. Most people want to go the opposite way.

    Either way, neat addition.

  • by tambourine_man on 6/8/17, 9:42 PM

    Just yesterday I was looking for the other way around, CVS->JSON