Bufio Scanner Streaming NDJSON
Parse newline-delimited JSON streams with bufio.Scanner, sizing buffers for long lines and isolating malformed records.
Code
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
)
type Event struct {
Kind string `json:"kind"`
Data int `json:"data"`
}
func parseNDJSON(ctx context.Context, r io.Reader) (valid int, rejected int, err error) {
scanner := bufio.NewScanner(r)
// 默认缓冲 64KiB,超长行会报 ErrTooLong,按业务上调
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 4*1024*1024)
line := 0
for scanner.Scan() {
if err := ctx.Err(); err != nil {
return valid, rejected, err
}
line++
raw := scanner.Bytes()
if len(raw) == 0 {
continue
}
var event Event
if jsonErr := json.Unmarshal(raw, &event); jsonErr != nil {
rejected++ // 坏行隔离计数,不中断整条流
continue
}
if event.Kind == "" {
rejected++
continue
}
valid++
}
return valid, rejected, scanner.Err()
}
func main() {
stream := `{"kind":"view","data":1}
{"kind":"click","data":2}
not-json-at-all
{"kind":"purchase","data":3}
`
valid, rejected, err := parseNDJSON(
context.Background(),
stringsReader(stream),
)
fmt.Println("valid:", valid, "rejected:", rejected, "err:", err)
}
func stringsReader(s string) io.Reader { return &stringReader{s: s} }
type stringReader struct{ s string }
func (r *stringReader) Read(p []byte) (int, error) {
if len(r.s) == 0 {
return 0, io.EOF
}
n := copy(p, r.s)
r.s = r.s[n:]
return n, nil
}A reading of the statements
Bufio Scanner Streaming NDJSON is built around the functions parseNDJSON, main, stringsReader and Read.
Repetition is confined to for scanner.Scan() — the rest of the file is straight-line. err is inspected at 1 call site before any result is trusted.
Points of care
- Nothing to install — the file works with what the standard library ships.
- Errors are ordinary return values; skipping the
errcheck means using a zero value silently.
Applying it
Signature: func (r *stringReader) Read(p []byte) (int, error)
The run emits 1 labeled section — valid: — one per step.
main calls 3 helpers in sequence: parseNDJSON, stringsReader and Read.
Statements that matter
func stringsReader(s string) io.Reader { return &stringReader{s: s} }
return valid, rejected, scanner.Err()
func (r *stringReader) Read(p []byte) (int, error) {
if len(r.s) == 0 {
return 0, io.EOF
}
n := copy(p, r.s)
r.s = r.s[n:]
return n, nil
}
Interactive Tools for This Topic
JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.NDJSON to JSON ConverterTurn newline-delimited JSON (JSON Lines) into a single JSON array, reporting the exact line where parsing fails.
FAQ
- Is there exception handling?
- No exceptions — `error` returns checked at 1 site, and callers branch on `err` first.
- What does Bufio Scanner Streaming NDJSON print when run?
- Running the file directly is enough — expect labeled output lines such as `valid:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.