npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsonlines/core

v1.0.2

Published

parse and stringify jsonlines files through streams

Downloads

13,845

Readme

jsonlines

npm package @jsonlines/core GitHub package.json dependency version (dev dep on branch) semantic-release

a Node.js library to parse, stringify jsonlines files as streams

Install

npm install @jsonlines/core
yarn add @jsonlines/core

Features Guide

stringify

require("stream")
  .Readable.from([{ v: 1 }, { v: 2 }])
  .pipe(require("@jsonlines/core").stringify())
  .pipe(require("fs").createWriteStream("mydata.jsonl"));

parse

require("fs")
  .createReadStream("mydata.jsonl")
  .pipe(require("@jsonlines/core").parse())
  .on("data", (data) => {
    console.log("parsed data: ", data);
  });
require("stream")
  .Readable.from([{ v: 1 }, { v: 2 }])
  .pipe(
    require("@jsonlines/core").stringify({
      stringify: myCustomStringifyFunction,
    }),
  )
  .pipe(require("fs").createWriteStream("mydata.jsonl"));
require("fs")
  .createReadStream("mydata.jsonl")
  .pipe(
    require("@jsonlines/core").parse({
      parse: myCustomParseFunction,
    }),
  )
  .on("data", (data) => {
    console.log("receive data: ", data);
  });

stringify to a .jsonl.gz

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

require("fs")
  .createReadStream("mydata.jsonl.gz")
  .pipe(
    require("@jsonlines/core").parse({
      gzip: true,
    }),
  )
  .on("data", (data) => {
    console.log("receive data: ", data);
  });

Usage

stringify

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:

{"v":"object1"}
{"name":"Lady Gaga","records":["Chromatica"]}
[1,2,3,4]
true
false
2020
-1
null
null
{"value":null}
[null,null]

stringify API

// 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:

export interface JsonLinesStringifyOptions<V> {
  /**
   * specify the encoding to encode string to buffer
   *
   * NOTE that [the standard jsonlines](http://jsonlines.org/)
   * requires `utf8` as file encoding
   *
   * Defaults to `Buffer.from` default encoding,
   * which is `utf8`.
   */
  encoding?: BufferEncoding;

  /**
   * specify a function to stringify values.
   * It accepts a value as parameter,
   * and should return a string or a Promise<string>.
   *
   * Defaults to `JSON.stringify`
   */
  stringify?: (v: V) => string | Promise<string>;

  /**
   * specify whether to gzip the output
   *
   * Omit or use `false` to disable gzip.
   * Use `true` to gzip with default options.
   * Or use an object as params for `require('zlib').createGzip`
   */
  gzip?: JsonLinesGzipOption;

  /**
   * specify the line ending to be used in the output
   *
   * NOTE that [the standard jsonlines](http://jsonlines.org/)
   * requires `\n` as line separator
   *
   * Defaults to `\n`
   */
  lineSep?: "lf" | "\n" | "crlf" | "\r\n";
}

parse

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

function parse(options?: JsonLinesParseOptions<V>): JsonLinesParseStream;

parse function accepts an optional object as options and returns an instance of JsonLinesParseStream.

Note that JsonLinesParseStream extends Duplex.

options:

export interface JsonLinesParseOptions<V> {
  /**
   * specify the encoding to decode buffer to string
   *
   * NOTE that [the standard jsonlines](http://jsonlines.org/)
   * requires `utf8` as file encoding
   *
   * Defaults to `utf8`
   */
  encoding?: BufferEncoding;

  /**
   * specify a function to parse json line.
   * It accepts a string as parameter,
   * and should return a value or a Promise<V>.
   *
   * Defaults to `JSON.parse`
   */
  parse?: (line: string) => V | Promise<V>;

  /**
   * specify whether to gunzip the source
   *
   * Omit or use `false` to disable gunzip.
   * Use `true` to gunzip with default options.
   * Or use an object as params for `require('zlib').createGunzip`
   */
  gzip?: JsonLinesGzipOption;

  /**
   * specify the line ending to be parsed
   *
   * NOTE that [the standard jsonlines](http://jsonlines.org/)
   * requires `\n` as line separator
   *
   * If set to `auto`, both `\n` and `\r\n` will be accepted
   *
   * Defaults to `\n`
   */
  lineSep?: "lf" | "\n" | "crlf" | "\r\n" | "auto";
}