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
What TabularJS extracts from TXT files
- One worksheet.
dataholds every line as an array of strings, including the header line, padded to the same length. - Double-quoted fields are handled as in CSV, so a quoted value can contain the delimiter or a line break.
columnswith the titles A, B, C...
Convert TXT to JSON in Node.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
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
{
"worksheets": [
{
"data": [
["id", "name"],
["1", "Alice"]
],
"columns": [{ "title": "A" }, { "title": "B" }]
}
]
}Complete output for a two-line pipe-delimited file.
Options
| Option | Effect for TXT |
|---|---|
| delimiter | Column separator, one character. Default is a tab for .txt files. |
| encoding | Text encoding. Default utf-8. TextDecoder labels work everywhere; DOS codepages such as cp437 and cp850 work in Node.js through iconv-lite. |
Limitations
- Fixed-width text (columns aligned with spaces) is not split into columns: there is no delimiter to split on.
- The delimiter must be a single character; a multi-character separator such as
||is not recognized. - No type conversion: every value is a string.
- DOS codepages need iconv-lite, which is used in Node.js only. In the browser, use an encoding that TextDecoder supports.
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?
- No external spreadsheet parser dependencies: faster installs, smaller bundles.
- One API in Node.js and modern browsers.
- Output is shape-compatible withJspreadsheet: render parsed data in a live workbook with one call.