1

I have a csv like this:

name, email
raja, "raja@gmail.com, xyz@gmail.com"

Note that email has two values.

I want my json to be like:

{
   "name": "raja"
    "email": ["raja@gmail.com","xyz@gmail.com"]
}

Is there any way to do it in papa parser ?

rajagopalx
  • 3,012
  • 9
  • 41
  • 52
  • `email` has an single string value. I don't think PapaParse has any builtin to reparse the strings it got, but if you're confident enough in your data format, it shouldn't be too hard to search for `,` characters in the parsed data and call `split(',').map(str=>str.trim())` – Kaiido Jun 26 '17 at 08:32
  • But I would advice you to use an other delimiter than the main one (i.e don't use `,`) – Kaiido Jun 26 '17 at 08:44
  • Thanks.. I will try. – rajagopalx Jun 26 '17 at 08:49

1 Answers1

2

The parser cannot handle your email array format, try to change to something like:

name,email
raja,raja@gmail.com|xyz@gmail.com

Inside config object do this:

var config = {
    //... other config settings
    complete: function (parsed) {
        parsed.data.forEach(row => Object.assign(row, {email: row.email.split("|")}))
    }
};
Vanojx1
  • 5,574
  • 2
  • 23
  • 37