Skip to main content
Module

std/json/mod.ts>JsonParseStream

Deno standard library
Go to Latest
class JsonParseStream
extends TransformStream<string, JsonValue>
import { JsonParseStream } from "https://deno.land/std@0.190.0/json/mod.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.190.0/streams/text_line_stream.ts";
import { JsonParseStream } from "https://deno.land/std@0.190.0/json/json_parse_stream.ts";

const url = "https://deno.land/std@0.190.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.190.0/streams/text_delimiter_stream.ts";
import { JsonParseStream } from "https://deno.land/std@0.190.0/json/json_parse_stream.ts";

const url =
  "https://deno.land/std@0.190.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)