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.

CSV to JSON is too fragile

See original GitHub issue

The CSV to JSON script is too fragile and breaks on real-world data such as double-quoted columns to contain commas and double-double-quotes for literal double-quotes.

I had a \r in some of my data which means the script isn’t standardizing to \n line endings.

Test:

Screen Shot 2020-07-20 at 15 44 11

Result:

Screen Shot 2020-07-20 at 15 44 29

Expected:

Screen Shot 2020-07-20 at 15 47 24

The JSON to CSV gets similarly confused in that it uses \" for literal double-quotes instead of the CSV convention of double-double-quotes within a quoted column.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Flare576commented, Jul 22, 2020

For what it’s worth: I grabbed the minified version of Papa Parse and pasted it straight into my csv_to_json.js, and it did work. I did have to take out the require and fix the code to be

function main(state) {
  try {
    const { data } = Papa.parse(state.text);
    state.text = JSON.stringify(data);
  }
  catch(error) {
    state.postError("Something strange happened here...");
  }
}

If the pattern Boop wants to follow is to have all dependencies in the lib folder, then creating a new file in there for Papa (with its LICENSE, of course) is probably the cleanest way, but before I throw up a PR I wanted to double-check that this is the approach you want.

0reactions
Flare576commented, Oct 29, 2020

Sorry for the double-post, but I just realized that there’s a combination of an assumption and a “Limitation” at play around the “header” stuff in this script.

First, we’re assuming that this data has a header (both in the original and in my fix). There’s a “Limitation” (and I use that term VERY loosely because I actually see this as a feature/simplicity) in Boop that you can’t pass parameters to the scripts.

So… the solutions I see are:

  1. Add another script and define one as CSV to JSON (headers), the other CSV to JSON (headerless)
  2. 🤮 Add some sort of design where the first line of state.text could be settings for a script 🤮
  3. ❓ Auto-detect header rows ❓ (I have NO idea how to do this and am pretty sure it’s a fool’s errand)

If we went with # 1, I’d argue that the 2nd script would just be an optional script, and the default would be headers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deal with CSV containing nested JSON objects (comma hell!)
Alas, my regex kung-fu is too weak to create something flexible enough based on the arbitrary nature of the JSON objects that may...
Read more >
How to convert a CSV file to JSON in 1 minute / CSV to JSON
A free, quick, and easy utility to upload your CSV file and convert to a JSON formatted text.
Read more >
Other ways to convert CSV to JSON (more performance)
Hello everybody,. I am using an Azure Logic app to convert a CSV file to JSON to transform the data into a SharePoint...
Read more >
Catch exception for transform.Unmarshal csv - support - HUGO
If you must use CSV, you could try to parse it yourself, but that will be very fragile. For example: {{ $map :=...
Read more >
Team Builder - PvPoke
Build your team for Pokemon GO Trainer Battles. See how your Pokemon match up offensively and defensively, discover which Pokemon are the best...
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