Action

TSV → JSON

Posted by Jørgen Arnor G. Lom, Last update about 3 years ago

Convert TSV to JSON.

Steps

  • script

    const selected = editor.getSelectedText();
    let query = '';
    let string = '';
    
    if (!selected || selected.length === 0){
       query = editor.getText();
    }else{
       query = selected;
    }
    
    function tsvJSON(tsv) {
      const lines = tsv.split("\n");
      const result = [];
      const headers = lines[0].split("\t");
    
      for (let i = 1; i < lines.length; i++) {
        const obj = {};
        const currentline = lines[i].split("\t");
    
        for (let j = 0; j < headers.length; j++) {
          obj[headers[j]] = currentline[j];
        }
    
        result.push(obj);
      }
    
      return result;
    }
    
    string = JSON.stringify(tsvJSON(query));
    
    if (!selected || selected.length === 0){
    	editor.setText(string);
    }else{
    	editor.setSelectedText(string);
    }
  • clipboard

    template
    [[draft]]

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.