parser
The parser module provides streaming decoders for CSV and NDJSON data.
It is designed for use with JSH streams and emits parsed objects through events.
Typical usage looks like this.
const parser = require('parser');Exported members
csv(options)ndjson(options)CSVParserNDJSONParser
csv()
Creates a CSV parser stream.
Syntax
parser.csv([options])Options
| Option | Type | Default | Description |
|---|---|---|---|
separator | String | , | Field separator |
quote | String | " | Quote character |
escape | String | same as quote | Escape character used for escaped quotes |
headers | true / false / String[] | true | Header handling mode |
skipLines | Number | 0 | Number of initial lines to ignore |
skipComments | Boolean | String | false | Skip comment lines; when a string is given it becomes the comment prefix |
strict | Boolean | false | Fail when a row has a different column count |
mapHeaders | Function | Maps header names | |
mapValues | Function | Maps field values before row emission | |
trimLeadingSpace | Boolean | true | Trim leading spaces from each field |
Return value
Returns a CSVParser instance.
CSVParser
CSV parser class exported by the module.
Creation
new parser.CSVParser([options])The constructor accepts the same options as parser.csv().
Events
headers: emitted once after the header row is parseddata: emitted for each parsed row objecterror: emitted when strict parsing failsend: emitted when the upstream stream finishes
Properties
bytesWritten: number of input bytes receivedbytesRead: number of bytes consumed by the parser
Row shape
- When
headersis omitted ortrue, the first non-skipped line becomes the header row. - When
headersisfalse, fields are exposed as"0","1","2", … - When
headersis an array, those names are used and the first line is treated as data. - In non-strict mode, extra columns are emitted as
_Nfields such as_3.
ndjson()
Creates an NDJSON parser stream.
Syntax
parser.ndjson([options])Options
| Option | Type | Default | Description |
|---|---|---|---|
strict | Boolean | true | Fail on invalid JSON lines instead of skipping them |
Return value
Returns an NDJSONParser instance.
NDJSONParser
NDJSON parser class exported by the module.
Creation
new parser.NDJSONParser([options])The constructor accepts the same options as parser.ndjson().
Events
data: emitted for each parsed JSON objectwarning: emitted for invalid lines whenstrict: falseerror: emitted when strict parsing failsend: emitted when the upstream stream finishes
warning event objects contain:
| Property | Type | Description |
|---|---|---|
line | Number | Line number of the skipped record |
data | String | Original trimmed line text |
error | String | Parse error message |
Properties
bytesWritten: number of input bytes receivedbytesRead: number of bytes consumed by the parser
CSV example
| |
NDJSON example
| |
Progress example
Both parser streams expose bytesWritten and bytesRead, so progress can be tracked while streaming.
| |
Behavior notes
- Both parser classes extend the JSH
stream.Transformimplementation. - Parsed rows and objects are emitted through
dataevents. - Empty lines are ignored by both parsers.
NDJSONParsertrims each line before parsing.CSVParserremoves a trailing\rso\r\ninput is handled correctly.
Last updated on