Skip to content

New way to readLines? #23495

Answered by magurotuna
alexgleason asked this question in Q&A
Apr 22, 2024 · 2 comments · 2 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/streams@0.223.0/text-line-stream";
import { JsonParseStream } from "jsr:@std/json@0.223.0/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 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

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

@iuioiua
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
4 participants