Open polRk opened 1 month 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.
I have an problem: i need to extract only json strings from response than contains array of simple objects: