Skip to content

New way to readLines? #23495

Answered by magurotuna
alexgleason asked this question in Q&A
Apr 22, 2024 · 2 comments · 4 replies
Discussion options

You must be logged in to vote

Here's what I'd do when I want to parse jsonl file from the filesystem:

import { TextLineStream } from "jsr:@std/[email protected]/text-line-stream";
import { JsonParseStream } from "jsr:@std/[email protected]/json-parse-stream";

using f = await Deno.open("./example.jsonl");
const readable = f.readable
  .pipeThrough(new TextDecoderStream()) // decode Uint8Array to string
  .pipeThrough(new TextLineStream()) // split string line by line
  .pipeThrough(new JsonParseStream()); // parse each chunk as JSON

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

Result:

❯ cat example.jsonl
───────┬─────────────────────────────────────────────────────────────────────────────────────────────…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@alexgleason
Comment options

@iuioiua
Comment options

@johndeighan
Comment options

@alexgleason
Comment options

Answer selected by alexgleason
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants