namespace
JSONL
namespace JSONL
JSONL (JSON Lines) related APIs.
Each line of the input is a JSON value.
interface ParseChunkResult
The result of
Bun.JSONL.parseChunk.- done: boolean
trueif all input was consumed successfully.falseif the input ends with an incomplete value or a parse error occurred. - error: null | SyntaxError
A
SyntaxErrorif a parse error occurred, otherwisenull. Values parsed before the error are still available invalues. - read: number
How much of the input was consumed. When the input is a string, this is a character offset. When the input is a
TypedArray, this is a byte offset. Useinput.slice(read)orinput.subarray(read)to get the unconsumed remainder.
- input: string | ArrayBufferLike | TypedArray<ArrayBufferLike> | DataView<ArrayBufferLike>): unknown[];
Parse a JSONL (JSON Lines) string into an array of JavaScript values.
If a parse error occurs and no values were successfully parsed, throws a
SyntaxError. If values were parsed before the error, returns the successfully parsed values without throwing.Incomplete trailing values (for example, from a partial chunk) are silently ignored.
When a
TypedArrayis passed, the bytes are parsed directly without copying if the content is ASCII.@param inputThe JSONL string or typed array to parse
@returnsAn array of parsed values
const items = Bun.JSONL.parse('{"a":1}\n{"b":2}\n'); // [{ a: 1 }, { b: 2 }] // From a Uint8Array (zero-copy for ASCII): const buf = new TextEncoder().encode('{"a":1}\n{"b":2}\n'); const items = Bun.JSONL.parse(buf); // [{ a: 1 }, { b: 2 }] // Partial results on error after valid values: const partial = Bun.JSONL.parse('{"a":1}\n{bad}\n'); // [{ a: 1 }] // Throws when no valid values precede the error: Bun.JSONL.parse('{bad}\n'); // throws SyntaxError - input: string | ArrayBufferLike | TypedArray<ArrayBufferLike> | DataView<ArrayBufferLike>,start?: number,end?: number
Parse a JSONL chunk, designed for streaming use.
Never throws on parse errors. Instead, returns whatever values were successfully parsed along with an
errorproperty containing theSyntaxError(ornullon success). Usereadto determine how much input was consumed anddoneto check if all input was parsed.When a
TypedArrayis passed, the bytes are parsed directly without copying if the content is ASCII. Optionalstartandendparameters select a window of the input without copying. For typed arrays these are byte offsets andreadis a byte offset into the original typed array. For strings these are character offsets andreadis a character offset into the original string.@param inputThe JSONL string or typed array to parse
@param startOffset to start parsing from (bytes for typed arrays, characters for strings, default: 0)
@param endOffset to stop parsing at (bytes for typed arrays, characters for strings, default: input length)
@returnsAn object with
values,read,done, anderrorpropertieslet buffer = new Uint8Array(0); for await (const chunk of stream) { buffer = Buffer.concat([buffer, chunk]); const { values, read, error } = Bun.JSONL.parseChunk(buffer); if (error) throw error; for (const value of values) handle(value); buffer = buffer.subarray(read); }