1

im a complete beginner to JS and need some help. I have a Textfile looking like this:

JOBID,NAME,USER,NODELIST,CPUS,STATE,REASON
2527,_DP-2-Q-095-84-1-50.job,loe_mk,,4,PENDING,launch failed requeued held
2528,_Q-095-76-2-05.job,fr_tho,,4,PENDING,launch failed requeued held
2515,_DC-3-V-095-76-0-10.job,pi_tim,node01,4,RUNNING,None

So JOBID, NAME and so on are the Names for the values below.

Now I want to parse it into a JSON object. I tried to do it like this:

var jdata = new Array();
jdata = data.toString().split('\n');
jsonstring = JSON.stringify(jdata);
fs.writeFile('out/data.json', jsObj, (err) => {
  if (err) throw err;
});

But the result is no JSON object right? I somehow need to attach connect the parameters to each other so it looks like:

{
    "JOBID": 2527,
    "NAME": '_DP-2-Q-095-84-1-50.job',
    ...
}

Somebody can tell me how to convert this correctly or isn't it even possible this way?

Thank you already

Dean Meehan
  • 2,511
  • 22
  • 36
floriantaut
  • 347
  • 1
  • 2
  • 11
  • Possible duplicate of [Convert CSV data into JSON format using Javascript](https://stackoverflow.com/questions/27979002/convert-csv-data-into-json-format-using-javascript) – Ehsan88 May 17 '18 at 10:26

3 Answers3

1

You've started correctly but you cannot simply use JSON.stringify(jdata); to convert to JSON. An example in pure JS is like so:

//Load in Input
var input = `JOBID,NAME,USER,NODELIST,CPUS,STATE,REASON
2527,_DP-2-Q-095-84-1-50.job,loe_mk,,4,PENDING,launch failed requeued held
2528,_Q-095-76-2-05.job,fr_tho,,4,PENDING,launch failed requeued held
2515,_DC-3-V-095-76-0-10.job,pi_tim,node01,4,RUNNING,None`;

//Split into Lines
var data = input.split("\n");
//Get all the header values
var header = input.split("\n")[0].split(",");

//Init Output Array
var output = [];

//For every row except the first (1...n)
for(var i=1;i<data.length;i++){
    //Get all the values
    var values = data[i].split(",");
    var obj = {};

    //For every value in the header
    for(var j=0;j<header.length;j++){
        //obj[JOBID] = 2527
        obj[header[j]] = values[j];
    }

    //Push to output
    output.push(obj);
}

Output now will equal your array of objects. You can then save it as you did before.

// [{"JobID": 2527, "...},{..}...]
jsonstring = JSON.stringify(output);
fs.writeFile('out/data.json', jsObj, (err) => {
  if (err) throw err;
});
Dean Meehan
  • 2,511
  • 22
  • 36
0

You are correct that you need to transform your data before it becomes a proper JSON object.

This is a way to do it (with a bit more modern Javascript):

const data = `JOBID,NAME,USER,NODELIST,CPUS,STATE,REASON
        2527,_DP-2-Q-095-84-1-50.job,loe_mk,,4,PENDING,launch failed requeued held
        2528,_Q-095-76-2-05.job,fr_tho,,4,PENDING,launch failed requeued held
        2515,_DC-3-V-095-76-0-10.job,pi_tim,node01,4,RUNNING,None
    `;

let lines = data.split('\n');
const headers = lines.shift().split(',');

// convert the individual lines to JSON objects
const jsonData = lines.map(line => {
  const parts = line.split(',');

  // Invalid lines - these are filtered out later
  if (parts.length !== headers.length) {
    return false;
  }

  // look up the name of the part from the header and use that as the property name
  return parts.reduce((acc, part, index) => ({
    ...acc,
    [headers[index]]: part,
  }), {});
}).filter(Boolean); // remove the invalid objects

console.log(jsonData);
jumoel
  • 1,770
  • 1
  • 13
  • 21
-1

Here is a simple approach and you can modify it as for your need

var data = 'JOBID,NAME,USER,NODELIST,CPUS,STATE,REASON\n'
+'2527,_DP-2-Q-095-84-1-50.job,loe_mk,,4,PENDING,launch failed requeued held\n'+
'2528,_Q-095-76-2-05.job,fr_tho,,4,PENDING,launch failed requeued held\n'+
'2515,_DC-3-V-095-76-0-10.job,pi_tim,node01,4,RUNNING,None';
var spdata = data.split('\n');
//assuming that the first row is always having columns names
var names = spdata[0].split(',');
var mainDataAr=[];
//reading data from the 2nd row
for(var i=1;i<spdata.length;i++)
{
  //taking the data from the row at position i
  var rdata = spdata[i].split(',');
  var obj={};
  for(var j=0;j<names.length;j++)
  {   
   obj[names[j]]=rdata[j]   
  }
  mainDataAr.push(obj);
}
console.log(mainDataAr);
vikscool
  • 1,293
  • 1
  • 10
  • 24
  • @floriantaut if the last line is always empty then you could use the first `for` loop as `for(var i=1;i – vikscool May 17 '18 at 10:50