title: How to take a TSV file and convert to JSON in JS
body: <p>I have a TSV file that has this in its file</p>
  
  <pre><code>Row ID  O ID    O Date  Ship Date   Ship Type   Customer ID Customer Name   Seg Country City    State   Zip Code    Region  Product ID  Category    Sub-Category    Product Name    Sales   Quantity    Discount    Profit
  1   PA-152156   11/9/20 01/19/16    Second Class    CG-125  Clay bute   Consumer    United States   Henderson   Kentucky    42420   South   FUR-BO-10001798 Furniture   Bookcases   Bush, Somerset Collection Bookcase? 261.96  2   0   41.9136
  2   PA-152156   11/18/20    01/19/16    Second Class    CG-120  Clay Bute   Consumer    United States   Henderson   Kentucky    42420   South   FUR-CH-10000454 Furniture   Chairs   Deluxe Fabric Upholstered Stacking Chairs, Rounded Back    731.94  3   0   219.582
  </code></pre>
  
  <p>That is just a sample. Theres a bit 100 lines items. I wanted to read this tsv file and display it as a JSON object in the format:</p>
  
  <pre><code>"Customer Name": {
          "orders": [
              {
                  "order_id": "CA-1234-567890",
                  "order_date": "YYYY-MM-DDTHH:MM:SS",
                  "line_items": [
                      {
                          "product_url": "https://www.fake.com/cat/subcat/123",
                          "business": 123.45
                      },
                      {
                          "product_url": "https://www.fake.com/cat/subcat/456",
                          "business": 67.89
                      },
                      ...
                  ]
              },
              ...
          ]
      },
      "Customer Name 2": { ... },
      ...
  }
  </code></pre>
  
  <p>where customer name must be whatever is in the tsv file value. And the url is constructed using the category, sub category and product id field. The business value is the value of sales. I am not sure how to do this in Javascript. This is what I have so far, but I am getting <code>[]</code> as an output:</p>
  
  <pre><code>function tsvJSON(tsv){
  
      var lines=tsv.split("\n");
  
      var result = [];
  
      var headers=lines[0].split("\t");
  
      for(var i=1;i&lt;lines.length;i++){
  
          var obj = {};
          var currentline=lines[i].split("\t");
  
          for(var j=0;j&lt;headers.length;j++){
              obj[headers[j]] = currentline[j];
          }
  
          result.push(obj);
  
      }
  
      //return result; //JavaScript object
      return JSON.stringify(result); //JSON
    }
  console.log(tsvJSON("./sampledata.tsv"));
  </code></pre>