0

I wrote the code that gets data from a device. The form of the data is csv. Below is the data value.

1,1.635946,1.636609,1.640240,1.636091
2,1.642825,1.640267,1.639013,1.636568
3,1.636835,1.636022,1.637664,1.637144
4,1.641332,1.641166,1.637950,1.640760
5,1.636041,1.637437,1.640702,1.633678

But I want the data in json format. So I tried using an online converter and got the following values:

[
 {
 "1": 2,
 "1.635946": 1.642825,
 "1.636609": 1.640267,
 "1.640240": 1.639013,
 "1.636091": 1.636568
 },
 {
 "1": 3,
 "1.635946": 1.636835,
 "1.636609": 1.636022,
 "1.640240": 1.637664,
 "1.636091": 1.637144
 }
]

What parts of my code should I modify if I want to get these values?

Below is my code.

var Timer;
var i = 0 ;
 setTimeout(function(){
 Timer = setInterval(function(){
 port.write(meascommand+'\n');
 i++;
 if(i==5){
 clearInterval(Timer);
 }
 },5000);
 },1000);
port.on('data',function(devicevalue){
 arrayvalue = devicevalue.toString();
 eachvalue = arrayvalue.split(';');
var results = [];
 var index = i ; 
 var ch0value = eachvalue[0] ; 
 var ch1value = eachvalue[1] ; 
 var ch2value = eachvalue[2] ; 
 var ch3value = eachvalue[3] ; 
 results[0] = index ;
 results[1] = ch0value ;
 results[2] = ch1value ;
 results[3] = ch2value ;
 results[4] = ch3value ;
 console.log(results);
 fs.appendFile(file,results+'\r\n',function(err){
 if(err)
 console.log(err);
 });
 });
};
link
2,4153 gold badges24 silver badges36 bronze badges
asked Mar 26, 2018 at 6:52
5
  • Modify this: eachvalue = arrayvalue.split(';'); to get sub-array of json Commented Mar 26, 2018 at 6:54
  • You first need to get rows, then you can get columns. Anyway, check this Commented Mar 26, 2018 at 6:58
  • @Justinas shoud i delete this line ? Commented Mar 26, 2018 at 6:59
  • you might want to check this gist Commented Mar 26, 2018 at 7:28
  • It seems to me you are missing headers for your data file. Each line above appears to be data lines. Add a first line like: line,val1,val2,val3,val4 Commented Mar 27, 2018 at 2:23

2 Answers 2

2
function processFiles(files) {
 var file = files[0];
 var reader = new FileReader();
 reader.onload = function (e) {
 var output = document.getElementById("fileOutput");
 var texto = e.target.result;
 csvJSON(texto);
 };
 reader.readAsText(file);
}
function csvJSON(csv) {
 var lines = csv.split("\n");
 var result = [];
 var headers;
 for (var i = 0; i < lines.length; i++) {
 headers = lines[i].split("\n");
 }
 var cont = 0;
 for (var i = 0; i < lines.length; i++) {
 var obj = {};
 var currentline = lines[i].split("\n");
 for (var j = 0; j < headers.length; j++) {
 obj[cont] = currentline[j];
 }
 cont++;
 result.push(obj);
 }
 return JSON.stringify(result); //JSON
}
answered Mar 26, 2018 at 6:57
Sign up to request clarification or add additional context in comments.

1 Comment

At least add a little description with a link to the npm package and project website.
0

Try to use below code. Credits go to: https://gist.github.com/iwek/7154578

NOTE: split(","). If lines contains , snippet won't work. But thats not the case in your data, as far as I can see.

function csvJSON(csv){
 var lines=csv.split("\n");
 var result = [];
 var headers=lines[0].split(",");
 for(var i=1;i<lines.length;i++){
 var obj = {};
 var currentline=lines[i].split(",");
 for(var j=0;j<headers.length;j++){
 obj[headers[j]] = currentline[j];
 }
 result.push(obj);
 }
 //return result; //JavaScript object
 return JSON.stringify(result); //JSON
}
answered Mar 26, 2018 at 7:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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