Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Latest
class JsonStringifyStream
extends TransformStream<unknown, string>
import { JsonStringifyStream } from "https://deno.land/std@0.224.0/json/json_stringify_stream.ts";

Convert each chunk to JSON string.

This can be used to stringify JSON lines, NDJSON, JSON Text Sequences, and Concatenated JSON.

You can optionally specify a prefix and suffix for each chunk. The default prefix is "" and the default suffix is "\n".

Examples

Example 1

import { JsonStringifyStream } from "https://deno.land/std@0.224.0/json/json_stringify_stream.ts";

const file = await Deno.open("./tmp.jsonl", { create: true, write: true });

ReadableStream.from([{ foo: "bar" }, { baz: 100 }])
  .pipeThrough(new JsonStringifyStream()) // convert to JSON lines (ndjson)
  .pipeThrough(new TextEncoderStream()) // convert a string to a Uint8Array
  .pipeTo(file.writable)
  .then(() => console.log("write success"));

To convert to JSON Text Sequences, set the prefix to the delimiter "\x1E" as options.

import { JsonStringifyStream } from "https://deno.land/std@0.224.0/json/json_stringify_stream.ts";

const file = await Deno.open("./tmp.jsonl", { create: true, write: true });

ReadableStream.from([{ foo: "bar" }, { baz: 100 }])
  .pipeThrough(new JsonStringifyStream({ prefix: "\x1E", suffix: "\n" })) // convert to JSON Text Sequences
  .pipeThrough(new TextEncoderStream())
  .pipeTo(file.writable)
  .then(() => console.log("write success"));

If you want to stream JSON lines from the server:

import { JsonStringifyStream } from "https://deno.land/std@0.224.0/json/json_stringify_stream.ts";

// A server that streams one line of JSON every second
Deno.serve(() => {
  let intervalId: number | undefined;
  const readable = new ReadableStream({
    start(controller) {
      // enqueue data once per second
      intervalId = setInterval(() => {
        controller.enqueue({ now: new Date() });
      }, 1000);
    },
    cancel() {
      clearInterval(intervalId);
    },
  });

  const body = readable
    .pipeThrough(new JsonStringifyStream()) // convert data to JSON lines
    .pipeThrough(new TextEncoderStream()); // convert a string to a Uint8Array

  return new Response(body);
});

Constructors

new
JsonStringifyStream(unnamed 0?: StringifyStreamOptions)

Constructs new instance.