Class JsonParseStream

Parse each chunk as JSON.

This can be used to parse JSON lines, NDJSON and JSON Text Sequences. Chunks consisting of spaces, tab characters, or newline characters will be ignored.

import { JsonParseStream } from "@std/json/parse-stream";
import { assertEquals } from "@std/assert";

const stream = ReadableStream.from([
`{"foo":"bar"}\n`,
`{"baz":100}\n`
]).pipeThrough(new JsonParseStream());

assertEquals(await Array.fromAsync(stream), [
{ foo: "bar" },
{ baz: 100 }
]);
import { TextLineStream } from "@std/streams/text-line-stream";
import { JsonParseStream } from "@std/json/parse-stream";
import { assertEquals } from "@std/assert";

const file = await Deno.open("json/testdata/test.jsonl");

const readable = file.readable
.pipeThrough(new TextDecoderStream()) // convert Uint8Array to string
.pipeThrough(new TextLineStream()) // transform into a stream where each chunk is divided by a newline
.pipeThrough(new JsonParseStream()); // parse each chunk as JSON

assertEquals(await Array.fromAsync(readable), [
{"hello": "world"},
["👋", "👋", "👋"],
{"deno": "🦕"},
]);
Hierarchy
  • TransformStream<string, JsonValue>
    • JsonParseStream

Constructors

Properties

Constructors

Properties

readable: ReadableStream<JsonValue>
writable: WritableStream<string>