What's the best way to split values when reading a...
# suitescript
p
What's the best way to split values when reading a CSV file if there are multiple commas on ex: description this is breaking the column numbers
e
p
thanks, is there a native way to do it in NS?
s
SuiteScript doesn't have a native CSV parsing module, but any JavaScript-based method will work, so you can use string methods like split, etc. Since CSV files use comma as a delimiter, text values with commas are usually surrounded by double quotes, so that the entire string is treated as one value (despite having commas in it). But doing this means that double quotes in a string value also have to be escaped properly, so they aren't treated as the beginning and end of a string value. Those are the high level issues you'd have to look for in building your own parser, but there are certainly edge-cases beyond those that make building your own parser quite difficult, unless you know the code producing the CSV file and exactly how it operates. That's why using a tested library like papaparse is the best way. It just works correctly and keeps your code clean so you can focus on other things like your business logic instead.
You could maybe get an AI code generator to spit out some JS CSV parsing code, but I'd trust an established library over that route
m
CSV files are complex enough to have their own standard (RFC 4180). Commas inside of text fields, single- or double-quotes inside of text fields. etc... PapaParse handles all of that for you. ^-- um....what he said. 🙂
s
Yeah, it's just not worth the effort, and I was once foolish enough to try it myself. It was a wasted effort, and only led to problems that consumed more of my time than just using the right tool from the start. The format seems simple, but has way too many complexities in the real world
m
I was just dealing with a 3rd party where we were regularly sending CSV files for import into their system and they didn't support double-quotes inside of text fields so I knew they were rolling their own CSV implementation. It was faster for us to remove all of the double quotes from the relevant fields and put in some logic to strip them when creating the CSV files than push them to fix their CSV implementation. :)
p
thanks for all the replies. I'll use papaparse then 🙂