jsonl
is a Go library to encode and decode JSON Lines. It uses the standard library's JSON package.
Go 1.20+ is required, because it relies on generics for type-safety.
See documentation for more details.
Use Writer
to encode items into JSON
before writing them to an underlying io.Writer
instance.
w := jsonl.NewWriter[*Item](dest)
if _, err := w.Write(item1, item2); err != nil {
log.Fatal(err)
}
Create a scanner with NewScanner
to iterate over a source one line at a time, or call the ReadAll
function to read all the lines at once.
Here's a example that uses Scanner
:
s := jsonl.NewScanner(src)
for s.Next() {
line, err := s.Line()
if err != nil {
log.Fatal(err)
}
var item Item
if err := line.Scan(&item); err != nil {
log.Fatal(err)
}
log.Println(item)
}
// Check if an error occurred while scanning.
if err := s.Err(); err != nil {
log.Fatalf("an error happened: %v", err)
}