batMan007
batMan007

Reputation: 589

how to form json structure in javascript

All, I have a module to import CSV file and fetch data and display in a grid. Fetched the data in array but I expected value must be in a particular JavaScript data structure.

Here my sample code

function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
console.log("headers-->"+headers)
var lines = [];

for (var i=1; i<allTextLines.length; i++) {
    var data = allTextLines[i].split(',');
    if (data.length == headers.length) {

        var tarr = [];
        for (var j=0; j<headers.length; j++) {
            tarr.push(headers[j]+":"+data[j]);
        }
        lines.push(tarr);
        // console.log(lines)
    }
}

console.log("details ="+lines)

});

allText Value

serial,Asset Type,id 
Asset1,Equipemnt,id1
Asset2,Equipemnt,id2
Asset3,Equipemnt,id3
Asset4,Equipemnt,id4

My output:

Serial:Asset1,Asset Type:Equipment,id:RF0001,
Serial:Asset2,Asset Type:Equipment,id:R0002,
Serial:Asset3,Asset Type:Equipment,id:R0003,
Serial:Asset4,Asset Type:Equipment,id:F0004,
Serial:Asset5,Asset Type:Equipment,id:F0005,
Serial:Asset6,Asset Type:Equipment,id:0006,
Serial:Asset7,Asset Type:Equipment,id:007,

Expected structure:

 {
    serial:["Asset1","Asset1","Asset2","Asset3","Asset4"],
    Asset Type:["Equipment","Equipment","Equipment","Equipment","Equipment"],
    id:["id1","id2","id3","id4",]
    }

How to achieve this structure?

Upvotes: 0

Views: 112

Answers (2)

Anika
Anika

Reputation: 160

function processData(allText) {
   var allTextLines = allText.split(/\r\n|\n/);
   var headers = allTextLines[0].split(',');
   console.log("headers-->"+headers)
   //initializing resulting json
   var lines = {};

   //initializing arrays for all the headers
   for(var i=0; i<headers.length; i++){
               lines[headers[i]] = [];
   }


   for (var i=1; i<allTextLines.length; i++) {
       var data = allTextLines[i].split(',');
       if (data.length == headers.length) {

           for (var j=0; j<headers.length; j++) {
              lines[headers[j]].push(data[j]);
          }

       }
   }
       return lines
   }




   //format all text is taken up (I tested on a string)
   allText = "serial,Asset Type,id\n Asset1,Equipemnt,id1\nAsset2,Equipemnt,id2\nAsset3,Equipemnt,id3\nAsset4,Equipemnt,id4"

Result : enter image description here

Upvotes: 2

Jack jdeoel
Jack jdeoel

Reputation: 4584

You first line is header and wanted to set as a key object . So you already get that var headers = allTextLines[0].split(','); thus assign that object key by object[key] = value format .

I assume that your header and lines splited key has the same thus I loop allTextLines and used the same key for both headers and allTextLines !!

var all = "serial,Asset Type,id \n"+
"Asset1,Equipemnt,id1 \n"+
"Asset2,Equipemnt,id2 \n"+
"Asset3,Equipemnt,id3 \n"+
"Asset4,Equipemnt,id4";

processData(all);

function processData(allText) {
var allTextLines = allText.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var lines = {};
//set headers as key
for(var i = 0 ; i < headers.length;i++) {
   lines[headers[i]] = [];
}
//assign relative value to key
for (var i=1; i<allTextLines.length; i++) {
    var data = allTextLines[i].split(',');
    for(var d in data) {
      lines[headers[d]].push(data[d]);
    }
}

console.log(lines)

}

Upvotes: 2

Related Questions