Skip to main content
Module

x/enzastdlib/vendor/@deno-std-json.ts>JsonParseStream

enzastdlib is a set of TypeScript modules that follow a common design API philosophy aiming at sane defaults and ease-of-use targeting the Deno TypeScript runtime.
Latest
class JsonParseStream
extends TransformStream<string, JsonValue>
import { JsonParseStream } from "https://deno.land/x/enzastdlib@v0.0.4/vendor/@deno-std-json.ts";

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.

Examples

parse JSON lines or NDJSON

import { TextLineStream } from "https://deno.land/std@0.224.0/streams/text_line_stream.ts";
import { JsonParseStream } from "https://deno.land/std@0.224.0/json/json_parse_stream.ts";

const url = "https://deno.land/std@0.224.0/json/testdata/test.jsonl";
const { body } = await fetch(url);

const readable = body!
  .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

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

parse JSON Text Sequences

import { TextDelimiterStream } from "https://deno.land/std@0.224.0/streams/text_delimiter_stream.ts";
import { JsonParseStream } from "https://deno.land/std@0.224.0/json/json_parse_stream.ts";

const url =
  "https://deno.land/std@0.224.0/json/testdata/test.json-seq";
const { body } = await fetch(url);

const delimiter = "\x1E";
const readable = body!
  .pipeThrough(new TextDecoderStream())
  .pipeThrough(new TextDelimiterStream(delimiter)) // transform into a stream where each chunk is divided by a delimiter
  .pipeThrough(new JsonParseStream());

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

Constructors

new
JsonParseStream(unnamed 0?: ParseStreamOptions)