parse and stringify jsonlines files through streams
npm install @jsonlines/core
![GitHub package.json dependency version (dev dep on branch)]()

a Node.js library to parse, stringify jsonlines files as streams
``shell`
npm install @jsonlines/core
yarn add @jsonlines/core
Easy to use. parse stream and stringify stream are standard node duplex streams
stringify
`js`
require("stream")
.Readable.from([{ v: 1 }, { v: 2 }])
.pipe(require("@jsonlines/core").stringify())
.pipe(require("fs").createWriteStream("mydata.jsonl"));
parse
`js`
require("fs")
.createReadStream("mydata.jsonl")
.pipe(require("@jsonlines/core").parse())
.on("data", (data) => {
console.log("parsed data: ", data);
});
Custom stringify / parse functions. Async function or function that returns a Promise is also supported.
`js`
require("stream")
.Readable.from([{ v: 1 }, { v: 2 }])
.pipe(
require("@jsonlines/core").stringify({
stringify: myCustomStringifyFunction,
}),
)
.pipe(require("fs").createWriteStream("mydata.jsonl"));
`js`
require("fs")
.createReadStream("mydata.jsonl")
.pipe(
require("@jsonlines/core").parse({
parse: myCustomParseFunction,
}),
)
.on("data", (data) => {
console.log("receive data: ", data);
});
Gzip / Gunzip
stringify to a .jsonl.gz
`js`
require("stream")
.Readable.from([{ v: 1 }, { v: 2 }])
.pipe(
require("@jsonlines/core").stringify({
gzip: true,
}),
)
.pipe(require("fs").createWriteStream("mydata.jsonl.gz"));
parse from a .jsonl.gz
`js`
require("fs")
.createReadStream("mydata.jsonl.gz")
.pipe(
require("@jsonlines/core").parse({
gzip: true,
}),
)
.on("data", (data) => {
console.log("receive data: ", data);
});
`js
const { stringify } = require("@jsonlines/core");
// or import from sub-module
const { stringify } = require("@jsonlines/core/stringify");
// or import with es module
import { stringify } from "@jsonlines/core";
import { stringify } from "@jsonlines/core/stringify";
require("stream")
.Readable.from([
// objects
{ v: "object1" },
{ name: "Lady Gaga", records: ["Chromatica"] },
// arrays
[1, 2, 3, 4],
// booleans
true,
false,
// numbers
2020,
-1,
// null
// Note that single null value can't be written to node streams,
// so @jsonlines/core provides a helper value to represent null
require("@jsonlines/core").nullValue,
require("@jsonlines/core/null-value").nullValue,
// note that it not necessary to use this helper value when null is in an array or object
{ value: null },
[null, null],
])
.pipe(
// create a stringify stream, which is a duplex stream
stringify(),
)
.pipe(process.stdout);
`
the output will be:
`jsonlines`
{"v":"object1"}
{"name":"Lady Gaga","records":["Chromatica"]}
[1,2,3,4]
true
false
2020
-1
null
null
{"value":null}
[null,null]
#### stringify API
`ts`
// prettier-ignore
function stringify(options?: JsonLinesStringifyOptions): JsonLinesStringifyStream;
stringify function accepts an optional object as options and returns an instance of JsonLinesStringifyStream.
Note that JsonLinesStringifyStream extends Duplex.
options:
`tsutf8
export interface JsonLinesStringifyOptions
/**
* specify the encoding to encode string to buffer
*
* NOTE that the standard jsonlines
* requires as file encodingBuffer.from
*
* Defaults to default encoding,utf8
* which is .
*/
encoding?: BufferEncoding;
/**
* specify a function to stringify values.
* It accepts a value as parameter,
* and should return a string or a Promise
*
* Defaults to JSON.stringify
*/
stringify?: (v: V) => string | Promise
/**
* specify whether to gzip the output
*
* Omit or use false to disable gzip.true
* Use to gzip with default options.require('zlib').createGzip
* Or use an object as params for
*/
gzip?: JsonLinesGzipOption;
/**
* specify the line ending to be used in the output
*
* NOTE that the standard jsonlines
* requires \n as line separator\n
*
* Defaults to `
*/
lineSep?: "lf" | "\n" | "crlf" | "\r\n";
}
`js
const { parse } = require("@jsonlines/core");
// or import from sub-module
const { parse } = require("@jsonlines/core/parse");
// or import with es module
import { parse } from "@jsonlines/core";
import { parse } from "@jsonlines/core/parse";
const source = require("stream").Readable.from({"v":"object1"}
{"name":"Lady Gaga","records":["Chromatica"]}
[1,2,3,4]
true
false
2020
-1
null
null
{"value":null}
[null,null]);
// create a parse stream, which is a duplex stream
const parseStream = parse();
source.pipe(parseStream);
// you can also consume it with for await ... of
parseStream.on("data", (value) => {
if (value === require("@jsonlines/core/null-value").nullValue)
console.log(--- The following value is nullValue ---);
console.log(value);
});
`
the output will be:
``
{ v: 'object1' }
{ name: 'Lady Gaga', records: [ 'Chromatica' ] }
[ 1, 2, 3, 4 ]
true
false
2020
-1
--- The following value is nullValue ---
[Object: null prototype] [Null] {}
--- The following value is nullValue ---
[Object: null prototype] [Null] {}
{ value: null }
[ null, null ]
#### parse API
`ts`
function parse(options?: JsonLinesParseOptions
parse function accepts an optional object as optionsJsonLinesParseStream
and returns an instance of .
Note that JsonLinesParseStream extends Duplex.
options:
`tsutf8
export interface JsonLinesParseOptions
/**
* specify the encoding to decode buffer to string
*
* NOTE that the standard jsonlines
* requires as file encodingutf8
*
* Defaults to
*/
encoding?: BufferEncoding;
/**
* specify a function to parse json line.
* It accepts a string as parameter,
* and should return a value or a Promise
*
* Defaults to JSON.parse
*/
parse?: (line: string) => V | Promise
/**
* specify whether to gunzip the source
*
* Omit or use false to disable gunzip.true
* Use to gunzip with default options.require('zlib').createGunzip
* Or use an object as params for
*/
gzip?: JsonLinesGzipOption;
/**
* specify the line ending to be parsed
*
* NOTE that the standard jsonlines
* requires \n as line separatorauto
*
* If set to , both \n and \r\n will be accepted\n
*
* Defaults to ``
*/
lineSep?: "lf" | "\n" | "crlf" | "\r\n" | "auto";
}