Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
split csv where fields contain commas

Thanks to the answer posted here: Put data from a csv file into an array (Javascript) I have been able to split a csv file in the manner I need to but I have come across a problem with the output. Nothing to do with the code as it works just as I wanted it to. The issue relates to the source data.

The csv files are provided to me as is so I did not realise that they had fields that contained commas. This means the split doesn't work as needed.

This is the code I am using:

$.get("export.csv", function process(dataString) {
    var lines = dataString
    .split(/\n/)
    .map(function(lineStr) {
        return lineStr.split(",");
    });
  
  var keys = lines[0];

  var objects = lines
    .slice(1)
    .map(function(arr) {
      return arr.reduce(function(obj, val, i) {
        obj[keys[i]] = val; 
        return obj;
      }, {});
    });
  
  console.log(objects);
})

This gives me the output in this format:

{
    "PROBLEM_NUMBER": "ticket_number",
    "CALLER_NAME": "\"surname",
    "PRIORITY": " forename\"",
    "CALL_TIME": "4",
    "CALL_DETAILS": "date",
    "RESOLVER": "group",
    "RESTORING_GROUP": "\"surname",
    "RESOLVING_GROUP": " forename\"",
    "RESTORATION_TIME": "group",
    "RAG_STATUS": "group",
    "CALL_STATUS": "date",
    "CALL_TYPE": "RED",
    "RESTORATION_CODE": "Closed",
    "SUBMITTER_GROUP": "Problem",
    "ASSIGNEE_GROUP": "resolution",
    "ASSIGNEE_NAME": "group",
    "RESOLVED_DATE_TIME": "group",
    "RESTORED_DATE_TIME": "",
    "TIME_WITH_TEAM": "date",
    "MONTH/YEAR\r": "date",
    "undefined": "Jan-21\r"
}

As you can see, the final field is "undefined" due to 2 fields containing a comma and splitting incorrectly.

I know I need to use regex to modify the split correctly however I don't understand or know where to put it. Is anyone able to assist me please?

Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I managed to solve the problem so thought I would post here for anyone else that may come across a similar issue:

To resolve it I declared the regex string as a variable and then called that in the .split() instruction.

The regex string I used was - /("[^"]+"|[^,]+)*,/g

My code now looks like this:

$.get("export.csv", function process(dataString) {
    var regex = /("[^"]+"|[^,]+)*,/g;
    var lines = dataString
    .split(/\n/)
    .map(function(lineStr) {
       return lineStr.split(regex);
    });
  var keys = lines[0];
  var objects = lines
    .slice(1)
    .map(function(arr) {
      return arr.reduce(function(obj, val, i) {
        obj[keys[i]] = val;
        return obj;
      }, {});
    });
  console.log(objects);
})

This gives me the correct output I need and maps all values accordingly

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!