cdauth / json-stream-es

A streaming JSON parser/stringifier using web streams.
BSD 2-Clause "Simplified" License
1 stars 1 forks source link

How to read json strings in array #3

Open polRk opened 1 month ago

polRk commented 1 month ago

I have an problem: i need to extract only json strings from response than contains array of simple objects:

new Response('[{}, {}, {}, ...]')...
// should return generator/stream of '{}', '{}', '{}'
cdauth commented 1 week ago

One way to do it would be to parse the JSON values and then stringify them again:

import { parseJsonStream } from "json-stream-es";

const stream = response
    .pipeThrough(new TextDecoderStream())
    .pipeThrough(parseJsonStream([]))
    .pipeThrough(new TransformStream({
        transform: (chunk, controller) => {
            controller.enqueue(JSON.stringify(chunk));
        }
    }));

Another way (I’m not sure if it is more or less performant than the above approach) that would also preserve the formatting of the JSON would be to manually combine the different transformers to split up the stream into single JSON value streams and stringify those again:

import { JsonParser, JsonPathDetector, JsonPathSelector, JsonPathStreamSplitter, streamToString } from "json-stream-es";

const stream = response
    .pipeThrough(new JsonParser())
    .pipeThrough(new JsonPathDetector())
    .pipeThrough(new JsonPathSelector((path) => path.length === 1))
    .pipeThrough(new JsonPathStreamSplitter())
    .pipeThrough(new TransformStream({
        transform: async (chunk, controller) => {
            controller.enqueue(await streamToString(chunk.pipeThrough(new JsonStringifier())));
        }
    });

I haven't tested this code, let me know if something doesn't work.