Live data from Hacker News

Show HN: Transform a CSV into a JSON and vice versa

jsonmatic.com

71–80 of 111 posts

Re: Show HN: Transform a CSV into a JSON and vice versa

#72

Earlier quoted context omitted.

So that we may have this discussion and get out of this strange rut that is the care and feeding of idempotent little formats. And to educate! To show each other. To make knowledge discoverable. That’s the reason websites like this are honestly a very good thing.

Thanks, AnthonBerg.

Kudos for doing the work! :bow:

I see an appropriate beauty in your username representing an information propagation model – of modern waves in modern habitats.

I do hope that my comment about a “rut” and “little formats” doesn’t disparage the work. I try to speak for enlightenment but sometimes I fall into lamenting the darkness.

Re: Show HN: Transform a CSV into a JSON and vice versa

#73
post #34

Looks a bit like adware IMO. The library appears to be drenched in analytics. Dependencies include: https://www.npmjs.com/package/web-vitals/v/0.1.0 https://www.npmjs.com/package/@fingerprintjs/fingerprintjs Harvesting user's data, most likely...

Author here; some clarification. I use fingerprint to get the number of visits (instead of using something invasive like Google Analytics): https://github.com/erikmartinjordan/jsonmatic/blob/master/sr... I get the fingerprint as a UID (which is like a random number for me). I don't harvest any user's data. Code is open-source, you can verify what I'm saying if you wish.

Collecting usage statistics is harvesting data. This is a classic example of why you should never run random NPM modules. Or even install them as all of this is possible in a post install script too.

Putting analytics in a deployed app is your prerogative. Putting it in what touts itself as a reusable component is at best frowned upon.

Re: Show HN: Transform a CSV into a JSON and vice versa

#75
post #73

Earlier quoted context omitted.

Author here; some clarification. I use fingerprint to get the number of visits (instead of using something invasive like Google Analytics): https://github.com/erikmartinjordan/jsonmatic/blob/master/sr... I get the fingerprint as a UID (which is like a random number for me). I don't harvest any user's data. Code is open-source, you can verify what I'm saying if you wish.

Collecting usage statistics is harvesting data. This is a classic example of why you should never run random NPM modules. Or even install them as all of this is possible in a post install script too. Putting analytics in a deployed app is your prerogative. Putting it in what touts itself as a reusable component is at best frowned upon.

Ok, I see your point. I update the website.

This commit deletes any kind of data harvesting (removes fingerprint and visit counting):

https://github.com/erikmartinjordan/jsonmatic/commit/7f3fa89...

Re: Show HN: Transform a CSV into a JSON and vice versa

#76
post #58

Earlier quoted context omitted.

CSV (and it's derivatives) made some sense 20 years ago but these days if you want to make your lives easier with tables you're better off using jsonlines. ["Name", "Session", "Score", "Completed"] ["Gilbert", "2013", 24, true] ["Alexa", "2013", 29, true] ["May", "2012B", 14, false] ["Deloise", "2012A", 19, true] Plenty of tools, including some I've written myself, support it. https://jsonlines.org/examples/ https://…

that's just CSV with unnecessary square brackets and whitespace

As someone who's written parsers for both CSV and jsonlines, I can assure you that you could not be further from the truth:

1. The whitespace is optional. It's just put there for illustrative purposes.

2. Whitespace in CSV can actually corrupt the data where some parsers make incompatible assumptions vs other CSV parsers. Eg space characters used before or after commas -- do you trim them or include them? Some will delimit on tabs as well as commas rather than either/or. Some handle new lines differently

3. Continuing off the previous point: new lines in CSVs, if you're following IBM's spec, should be literal new lines in the file. This breaks readability and it makes streaming CSVs more awkward (because you then break the assumption that you can read a file one line at a time). jsonlines is much cleaner (see next point).

4. Escaping is properly defined. eg how do you escape quotation marks in CSV? IBM's CSV spec states double quotes should be doubled up (eg "Hello ""world""") where as some CSV parsers prefer C-style escaping (eg "Hello \"world\"") and some CSV parsers don't handle that edge case at all. jsonlines already has those edge cases solved.

5. CSV is data type less. This causes issues when importing numbers in different parsers (eg "012345" might need to be a string but might get parsed as an integer with the leading space removed. Also should `true` be a string or a boolean? jsonlines is typed like JSON.

The entire reason I recommend jsonlines over CSV is because jsonlines has the readability of CSV while having the edge cases covered that otherwise leads to data corruption in CSV files (and believe me, I've head to deal with a lot of that over my extensive career!)

Re: Show HN: Transform a CSV into a JSON and vice versa

#77

this can turn an HTML table into JSON with the option to download the JSON, or it can turn JSON into an HTML table with no option to download a CSV -- where does CSV come into this? also, clearly javascript is a bit too ambitious for the job when e.g. PHP could provide the intended functionality with two lines of code: foreach($arrays as $values){echo implode(',', $values) . "\n";} echo json_encode($arrays); also, CS…

If you’re using JSON for tables then you’re much better off using jsonlines. It’s got a properly defined specification (unlike the wishy washy spec of CSV which every maintainer seems to implement differently) so you’re less likely to garble your data while still having all the benefits that CSVs do. Plus a lot of JSON marshaller will natively support jsonlines despite not advertising that functionality.

Personally I’d recommend jsonlines over regular CSVs these days but I’ve had so many issues with CSV parsers being incompatible over the years that I’d welcome anything which offers stricter formatting rules.

I’d definitely recommend you check it out. https://jsonlines.org

Re: Show HN: Transform a CSV into a JSON and vice versa

#78
`jq` can transform CSV to JSON and vice-versa, especially for simple/naive data where simply splitting on `,` is good enough - and where you aren't too bothered by types (e.g. if you don't mind numbers ending up as strings).

First attempt is to simply read each line in as raw and split on `,` - sort of does the job of, but it isn't the array of arrays that you might expect:

    $ echo -e "foo,bar,quux\n1,2,3\n4,5,6\n7,8,9" > foo.csv

    $ jq -cR 'split(",")' foo.csv
    ["foo","bar","quux"]
    ["1","2","3"]
    ["4","5","6"]
    ["7","8","9"]
Pipe that back to `jq` in slurp mode, though:

   $ jq -R 'split(",")' foo.csv | jq -cs
   [["foo","bar","quux"],["1","2","3"],["4","5","6"],["7","8","9"]]
And if you prefer objects, this output can be combined with the csv2json recipe from the jq cookbook[0], without requiring `any-json` or any other external tool:

   $ jq -cR 'split(",")' foo.csv | jq -csf csv2json.jq
   [{"foo":1,"bar":2,"quux":3},
    {"foo":4,"bar":5,"quux":6},
    {"foo":7,"bar":8,"quux":9}]
Note that this recipe also keeps numbers as numbers!

In the reverse direction there's a builtin `@csv` format string. This can be use with the second example above to say "turn each array into a CSV row" like so:

   $ jq -R  'split(",")' foo.csv | jq -sr '.[]|@csv'
   "foo","bar","quux"
   "1","2","3"
   "4","5","6"
   "7","8","9"
And to turn the fuller structure from the third example back into CSV, you can pick out the fields, albeit this one is less friendly with quotes and doesn't spit out a header (probably doable by calling `keys` on `.[0]` only...):

    $ jq -cR 'split(",")' foo.csv | jq -csf csv2json.jq | \
    > jq -r '.[]|[.foo,.bar,.quux]|@csv'
    1,2,3
    4,5,6
    7,8,9
I don't consider myself much of a jq power user, but I am a huge admirer of its capabilities.

[0] https://github.com/stedolan/jq/wiki/Cookbook#convert-a-csv-f...

Re: Show HN: Transform a CSV into a JSON and vice versa

#79

At work we work an a Python Library to do this, and much more: PyPi: https://pypi.org/project/flattentool/ Source: https://github.com/OpenDataServices/flatten-tool Docs: https://flatten-tool.readthedocs.io/en/latest/ It converts JSON to CSV and vice versa but also Spreadsheet files, XML ... It has recently had some work to make it memory efficient for large files. Work, BTW, is an Open Data Workers Co-op working on d…

What are the advantages of this tool in comparison with e.g. pandas' json_normalize?

Re: Show HN: Transform a CSV into a JSON and vice versa

#80
post #78

`jq` can transform CSV to JSON and vice-versa, especially for simple/naive data where simply splitting on `,` is good enough - and where you aren't too bothered by types (e.g. if you don't mind numbers ending up as strings). First attempt is to simply read each line in as raw and split on `,` - sort of does the job of, but it isn't the array of arrays that you might expect: $ echo -e "foo,bar,quux\n1,2,3\n4,5,6\n7,8,…

This is the kind of comment I came here for.
Post reply on HN