Convert XRS to Json

Viewed 41

I am trying to write js program (learning js at the same time) to convert an XRS file to JSON. I want to load an xrs file and output a JSON download

The file is loaded and split into an array

0: ["Mark Qty Finish Section Size Grade Length(mm)Area(m) Wt(Kg) "] 1: ["22800 1 S2 UB406*140*39 S355J0 6273 9.3 275.006 "] 2: ["22801 1 S2 UB406*140*39 S355J0 7112 9.7 290.443 "]

I get an error as the array is an object, it needs to be a string

let headerjson = [];
let datajson = [];
let datajson1 = [];

let header = {
  customer:"",
  doctitle:"",
  author:"",
  contract:"",
  date:'01.02.2018',
  client:"",
  draughtsman: "",
  contractname:"",
  phase:"",
}

const input = document.querySelector('input[type="file"]')
input.addEventListener('change', function (e){
  const reader = new FileReader()
  reader.onload = function (){
    const lines = reader.result.split('\n').map(function (line){
      return line.split(',')
    })
    getdata(lines);
  }
  reader.readAsText(input.files[0])
}, false)


function trimArray(arr){
    for(i=0;i<arr.length;i++)
    {
        arr[i] = arr[i].replace(/^\s\s*/, '').replace(-/\s\s*$/, '');
    }
    return arr;
}

function makeheader(){
  //console.log(headerjson)

  var tempstore = arrayToString(headerjson[0]);
  header.customer =   tempstore.slice(0,13).trim();
  header.doctitle =  tempstore.slice(29,39).trim();
  header.author =  tempstore.slice(39,49).trim();

  var tempstore = arrayToString(headerjson[1]);
  header.contract = + tempstore.slice(15,25).trim();
  header.date = tempstore.slice((tempstore.length-11),tempstore.length).trim();

  var tempstore = arrayToString(headerjson[2]);
  header.client =  tempstore.slice(15,35).trim();
  header.draughtsman =  tempstore.slice(67).trim();

  var tempstore = arrayToString(headerjson[3]);
  header.contractname =  tempstore.slice(15,35).trim();
  header.phase  =  tempstore.slice(67).trim();
}

function makedata(){
  splitdata(datajson)
}

function arrayToString(arr) {
  let str = '';
  arr.forEach(function(i, index) {
    str += i;
    if (index != (arr.length - 1)) {
      str += ',';
    };
  });
  return str;
}

function getdata(linesc){

  for (var i = 2; i < 7; i++){
    if (i != 3){
      headerjson.push(trimArray(linesc[i]));
    }
  }

  makeheader();

  // removes unwanted lines and cleans up the data

  totallines = (linesc.length);
  totallines = totallines -4;  // remove 4 extra lines
  datalines = 60;
  linescount = 0;
  startpoint = 10;

  datajson.push(trimArray(linesc[8])); //header information

  for (var i = startpoint; i < (totallines); i = i + 2){
      datajson.push(trimArray(linesc[i]));

      if( linescount == datalines){
        i = i + 12;
        linescount = 0;
      }
      else{
        linescount = linescount +2;
      }
  }

  console.log(datajson);
  makedata();
}

function splitdata(str){
  console.log(str);
  str = arrayToString(str);
    console.log(str);

var cells = str.split('\n').map(function (el) { return el.split(/\s+/); });
var headings = cells.shift();
var out = cells.map(function (el) {
var obj = {};

  for (var i = 0, l = el.length; i < l; i++) {
    obj[headings[i]] = isNaN(Number(el[i])) ? el[i] : +el[i];
  }
  return obj;
});
console.log(cells)
//console.log(JSON.stringify(out, null, 2));
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS FileReader</title>
</head>
<body>

  <input type="file" />

  <script src="js/bundle.js" charset="utf-8"></script>
</body>
</html>

this is a sample XRS file format


BDC Ltd PART LIST (By Mark) Page: 1


Contract No : 79 Date : 09.07.2018 Client : Big Builders Draughtsman: MULTI Contract Name: St James - bubblington Phase : 22B_


Mark Qty Finish Section Size Grade Length(mm)Area(m²) Wt(Kg)


22800 1 S2 UB406*140*39 S355J0 6273 9.3 275.006


22801 1 S2 UB406*140*39 S355J0 7112 9.7 290.443


22802 1 S2 UB406*140*39 S355J0 7313 10.0 298.376


22803 1 S2 UB406*140*39 S355J0 7253 10.1 291.541


22804 1 S2 UB406*140*39 S355J0 7112 9.7 290.443


22805 1 S2 UB406*140*39 S355J0 7294 10.0 298.579


22806 1 S2 UB406*140*39 S355J0 7313 10.0 298.376


22807 1 S2 UB406*140*39 S355J0 7095 9.7 293.354


22808 1 S2 UB406*140*39 S355J0 7095 9.9 295.709


22809 1 S2 UB406*140*39 S355J0 7481 10.2 301.355


BDC Ltd PART LIST (By Mark) Page: 2


0 Answers
Related