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-web

v1.2.1

Published

Web stream based jsonlines decoder/encoder.

Downloads

405

Readme

jsonlines-web

deno module deno doc npm version ci codecov GitHub Sponsors

Web stream based jsonlines decoder/encoder

  • ✅Deno
  • ✅browser
  • ✅Node.js

This library supports JSON in the following formats:

  • Line-delimited JSON (JSONLinesParseStream)
    • NDJSON
    • JSON lines
  • Record separator-delimited JSON (JSONLinesParseStream)
  • Concatenated JSON (ConcatenatedJSONParseStream)

See wikipedia for the specifications of each JSON.

install or import

Deno

https://deno.land/x/jsonlines/ https://doc.deno.land/https://deno.land/x/jsonlines/mod.ts

import {
  ConcatenatedJSONParseStream,
  ConcatenatedJSONStringifyStream,
  JSONLinesParseStream,
  JSONLinesStringifyStream,
} from "https://deno.land/x/[email protected]/mod.ts";

browser

import {
  ConcatenatedJSONParseStream,
  ConcatenatedJSONStringifyStream,
  JSONLinesParseStream,
  JSONLinesStringifyStream,
} from "https://deno.land/x/[email protected]/js/mod.js";

Node.js

https://www.npmjs.com/package/jsonlines-web

npm install jsonlines-web
import {
  ConcatenatedJSONParseStream,
  ConcatenatedJSONStringifyStream,
  JSONLinesParseStream,
  JSONLinesStringifyStream,
} from "jsonlines-web";
// if you need
// import { TextDecoderStream, TextEncoderStream } from "node:stream/web";
// import { fetch } from "undici";

Usage

A working example can be found at ./testdata/test.ts.

How to parse JSON Lines

./json-lines.jsonl

{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}
import { JSONLinesParseStream } from "https://deno.land/x/[email protected]/mod.ts";

const { body } = await fetch(
  "https://deno.land/x/[email protected]/testdata/json-lines.jsonl",
);

const readable = body!
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new JSONLinesParseStream());

for await (const data of readable) {
  console.log(data);
}

How to parse json-seq

./json-seq.json-seq

{"some":"thing\n"}
{
  "may": {
    "include": "nested",
    "objects": [
      "and",
      "arrays"
    ]
  }
}
import { JSONLinesParseStream } from "https://deno.land/x/[email protected]/mod.ts";

const { body } = await fetch(
  "https://deno.land/x/[email protected]/testdata/json-seq.json-seq",
);

const recordSeparator = "\x1E";
const readable = body!
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new JSONLinesParseStream({ separator: recordSeparator }));

for await (const data of readable) {
  console.log(data);
}

How to parse concat-json

./concat-json.concat-json

{"foo":"bar"}{"qux":"corge"}{"baz":{"waldo":"thud"}}
import { ConcatenatedJSONParseStream } from "https://deno.land/x/[email protected]/mod.ts";

const { body } = await fetch(
  "https://deno.land/x/[email protected]/testdata/concat-json.concat-json",
);

const readable = body!
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new ConcatenatedJSONParseStream());

for await (const data of readable) {
  console.log(data);
}

How to stringify JSON Lines

import { readableStreamFromIterable } from "https://deno.land/[email protected]/streams/mod.ts";
import { JSONLinesStringifyStream } from "https://deno.land/x/[email protected]/mod.ts";

const file = await Deno.open(new URL("./tmp.concat-json", import.meta.url), {
  create: true,
  write: true,
});

readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
  .pipeThrough(new JSONLinesStringifyStream())
  .pipeThrough(new TextEncoderStream())
  .pipeTo(file.writable)
  .then(() => console.log("write success"));

How to stringify json-seq

import { readableStreamFromIterable } from "https://deno.land/[email protected]/streams/mod.ts";
import { JSONLinesStringifyStream } from "https://deno.land/x/[email protected]/mod.ts";

const recordSeparator = "\x1E";
const file = await Deno.open(new URL("./tmp.concat-json", import.meta.url), {
  create: true,
  write: true,
});

readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
  .pipeThrough(new JSONLinesStringifyStream({ separator: recordSeparator }))
  .pipeThrough(new TextEncoderStream())
  .pipeTo(file.writable)
  .then(() => console.log("write success"));

How to stringify concat-json

import { readableStreamFromIterable } from "https://deno.land/[email protected]/streams/mod.ts";
import { ConcatenatedJSONStringifyStream } from "https://deno.land/x/[email protected]/mod.ts";

const file = await Deno.open(new URL("./tmp.concat-json", import.meta.url), {
  create: true,
  write: true,
});

readableStreamFromIterable([{ foo: "bar" }, { baz: 100 }])
  .pipeThrough(new ConcatenatedJSONStringifyStream())
  .pipeThrough(new TextEncoderStream())
  .pipeTo(file.writable)
  .then(() => console.log("write success"));

note

This library contains ReadableStream.prototype[Symbol.asyncIterator] polyfills. Importing this library will automatically enable ReadableStream.prototype[Symbol.asyncIterator].

develop

need to manually deno task transpile before release.