opyeb logscrub

Aggregating JSON Lines logs

2026-11-29 · 2 min read

One-line answer: JSON Lines puts one JSON object per line, which makes it the easiest log format to read line by line and aggregate by field.

The format rules

JSON Lines (.jsonl, .ndjson) stores one complete JSON value per line. The newline is the delimiter; there is no surrounding array and no comma. Files are UTF-8 and empty lines can be ignored.

Because each line is a complete object, the file can be streamed, and a broken line does not invalidate the rest. It also suits append-only storage of large logs.

{"ts":"2026-11-29T09:00:01Z","http":{"method":"GET","url":"/api/v1/orders/4821","status_code":200},"duration_ms":184}

Naming fields

Fields used for aggregation need a fixed name and type: an RFC 3339 timestamp, an integer status, a numeric duration_ms and a path without the query. Mixing strings and numbers breaks sorting and sums.

Nest at most two levels. http.status_code is clear, but five levels deep each tool spells the path differently. Keep correlation values such as request_id at the top level.

Suggested field names
MeaningNameType
TimetsRFC 3339 string
Levellevelinfo, warn, error
Methodhttp.methodstring
Pathhttp.urlstring without query
Statushttp.status_codeinteger
Durationduration_msnumber
Clientclient_ipstring
Request idrequest_idstring

Aggregating with jq

jq reads JSON Lines line by line and can select and aggregate fields. group_by gives the status distribution; sorting per-path means gives the slowest endpoints. -s reads all lines into one array, which group_by needs.

Numbers in the path split /orders/4821 and /orders/4822 into separate rows. Replace numeric segments with :id with gsub before grouping to get a meaningful ranking.

# Count by status class
jq -s 'group_by(.http.status_code / 100 | floor)
  | map({class: (.[0].http.status_code / 100 | floor), n: length})' app.jsonl

# Slowest paths by mean duration
jq -s 'map(. + {path: (.http.url | gsub("/[0-9]+"; "/:id"))})
  | group_by(.path)
  | map({path: .[0].path, n: length, avg: (map(.duration_ms) | add / length)})
  | sort_by(-.avg) | .[:10]' app.jsonl

Where results get distorted

A status stored as a string cannot be used in arithmetic. A missing field filled with zero pulls the mean down, so count missing values separately. A duration whose unit is not in its name can invert the ranking. Nested path spellings differ between collectors, so normalise http.status_code and http.statusCode to one form.

Broken lines should be counted, not silently dropped. A sudden rise in unparsable lines is often the first sign that a deployment changed the log format.

Check before sharing

JSON Lines still carries personal data. client_ip, user.email, authorization and query strings are the usual places. Field names make key-level masking easy, but free text in msg and nested headers need a separate look.

Even an aggregated table can contain an e-mail or order number in a path. Normalise and mask the path before sharing the table.

{"path":"/users/m***@example.com/profile","n":12,"avg_ms":184}

Questions

How is JSON Lines different from a JSON array?

An array must be read as a whole and one syntax error invalidates the file. JSON Lines is read and written line by line and a broken line can be skipped.

Can I aggregate without jq?

Yes. Paste JSON Lines into the workbench and the status distribution and slow or failing endpoints are computed in the browser. jq is the better fit for automated pipelines.

Sources

Every example here can be pasted into the workbench as it is.

Open the workbench