-2

Can any one help me in converting csv to json file. I am trying to write the code using Node js.

I have input file with two columns as:

Name    ,abc de
Role    ,role1; 
        ,role2
Address ,7, xyz 
        ,suburb 
        ,city, state
        ,country
///
Name    ,abc1 de1
Role    ,role3;
        ,role4 
Address ,8, xyz1 
        ,suburb1 
        ,city1, state1
        ,country1
///

I want the output as json:

[
    {Name:{first_name:abc, last_name:de}, 
    Role:[role1,role2], 
    Address:[7, xyz, suburb, city, state, country]},
    {Name:{first_name:abc1, last_name:de1},
    Role:[role3,role4], 
    Address:[8, xyz1, suburb1, city1, state1, country1]}
]

Many thanks in advance.
8
  • npmjs.com/package/csvtojson?activeTab=readme Commented Jul 19, 2021 at 7:15
  • If I look at input file, roles are separated by semicolon and address is separated by comma and attribute name is probably by tab, is the understanding correct? Can we have better input file or that's outside of your control? Commented Jul 19, 2021 at 7:17
  • I already tried with it. I couldn't get the expected output. Commented Jul 19, 2021 at 7:19
  • I have made changes to input file. Yes roles are separated with semicolon, address are separated by commas with multiple lines, first name and last name are separated by 20 spaces. Commented Jul 19, 2021 at 7:26
  • first name and last name are separated by 20 spaces??? Commented Jul 19, 2021 at 7:30

1 Answer 1

0

You could start with something like this, but I still think we need a custom implementation to process each block, we may not bale to do it in one function

var res = {};
var yourJsonString = `Name    ,abc de
Role    ,role1; 
        ,role2
Address ,7, xyz 
        ,suburb 
        ,city, state
        ,country`;
 res = yourJsonString.split('\n').map(processLine); 
 function processLine(data)
 {
       if(data.split(',')[0].indexOf("Name")>-1)
        { 
          return {Name: {first_name: data.split(',')[1].split(' ')[0],last_name:data.split(',')[1].split(' ')[1]}};
        }
 } 
 document.write('<pre>' + JSON.stringify(res, 0, 2) + '</pre>');

Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks for that. Can I please know how do I join two new lines (example: for role/ address)? I did some thing like this: else if (data.split(',')[0].indexOf("Role")>-1){ return {Role:[data.split(',')[1].split(';')[0]]};
I think what we need is go through all lines and push data to separate arrays, for example all names array, roles array and address array and then use map function to process each type of array and then join them
I could do everything other than joining two lines. I am actually using fs.createReadStream(inPath). After I read first line, I couldn't able to move to second line. if (line[0] == 'NAME') { line = addName(line); outData.push(line); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.