All formats

TXT to JSONPlain Text

Tab-delimited text files.

Treats .txt files as tab-delimited data by default, ideal for legacy text dumps and exports from older business software. Pass a delimiter for pipe- or semicolon-separated files.

Many older business systems, mainframe jobs and report generators write tables as .txt files: one record per line, with a tab, a pipe or a semicolon between fields. Excel's "Text (Tab delimited)" save option produces the same kind of file.

TabularJS reads .txt files with its CSV parser. The delimiter is a tab unless you pass another one, so a pipe-separated dump only needs { delimiter: '|' }. The result is the usual worksheets array with the rows as strings.

Feature support

Data values
Formulas
Styles
Merged cells
Comments
Column metadata

What TabularJS extracts from TXT files

Convert TXT to JSON in Node.js

js
import tabularjs from 'tabularjs';

// Tab-delimited (the default for .txt)
const tabs = await tabularjs('export.txt');

// Pipe-delimited dump from an old system, written in the DOS codepage 850
const dump = await tabularjs('dump.txt', { delimiter: '|', encoding: 'cp850' });
console.log(dump.worksheets[0].data);
// [ [ 'id', 'name' ], [ '1', 'Alice' ] ]

Read TXT files in the browser

js
import tabularjs from 'tabularjs';

const input = document.querySelector('input[type=file]');

input.addEventListener('change', async () => {
  const result = await tabularjs(input.files[0], { delimiter: '|' });
  console.log(result.worksheets[0].data);
});

JSON output

json
{
  "worksheets": [
    {
      "data": [
        ["id", "name"],
        ["1", "Alice"]
      ],
      "columns": [{ "title": "A" }, { "title": "B" }]
    }
  ]
}

Complete output for a two-line pipe-delimited file.

Options

OptionEffect for TXT
delimiterColumn separator, one character. Default is a tab for .txt files.
encodingText encoding. Default utf-8. TextDecoder labels work everywhere; DOS codepages such as cp437 and cp850 work in Node.js through iconv-lite.

Limitations

Frequently asked questions

How do I parse a pipe-delimited text file?

Pass the delimiter: await tabularjs('dump.txt', { delimiter: '|' }).

Can TabularJS read fixed-width text files?

No. Each line becomes a single value when there is no delimiter. Split fixed-width lines with String.slice() at the column positions instead.

My .txt file is comma-separated. What should I do?

Pass { delimiter: ',' }, or rename the file to .csv. The parsing rules are the same.

Is the first line used as a header?

No. The first line stays in data as row 0 and the columns are titled A, B, C... Take it out yourself with const [header, ...rows] = result.worksheets[0].data; when the file has a header line.

How do I read an old DOS export with accented characters?

Pass the DOS codepage, for example { encoding: 'cp850' } for Western European or { encoding: 'cp437' } for US files. This works in Node.js, where iconv-lite is installed as an optional dependency.

Why TabularJS for Plain Text?