GrepBoard/Manuals
605 manuals ·

journalctl -o json and jq: count entries by unit and priority

·Tested on Ubuntu 24.04 (Docker) · systemd 255 · jq 1.7 · 2026-10·Test scope ↓·7 steps

Pipe journalctl -o json into jq to count entries per unit and priority, trim output with --output-fields, and handle array and null fields (sample journal).

grep works on journalctl's human output, but grouping by fields that never appear in the message (unit, priority) is easier with -o json. Each line is one entry, so pull the field with jq and run sort | uniq -c.

The output below comes from a journal file built for testing (demo.journal, 1,203 entries over 70 minutes), which is why every command carries --file demo.journal. To count your server's system journal, drop just that part. Only root and members of the systemd-journal or adm groups can read the whole system journal, so add sudo if needed.

[1] Pin the time window. Without --until, new entries arrive between commands and every total comes out different.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | wc -l1203

Set S and U beforehand to past times, for example S="2026-10-27 12:00:00"; U="2026-10-27 13:10:00".

[2] Export only the fields you need. With --output-fields everything else is dropped, so jq has less to read.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | wc -c527962
console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=_SYSTEMD_UNIT,PRIORITY | wc -c449255

The test file has only about seven fields per entry, so it shrank by roughly 15%. A real system journal adds fields such as _PID, _CMDLINE and _HOSTNAME to every entry, so the reduction is usually larger. Something remains even with two fields because, as documented, __CURSOR, __REALTIME_TIMESTAMP, __MONOTONIC_TIMESTAMP and _BOOT_ID are always included. On this version (255) __SEQNUM and __SEQNUM_ID came along too.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=_SYSTEMD_UNIT,PRIORITY -n 1 | jq .{  "_BOOT_ID": "0123456789abcdef0123456789abcdef",  "__MONOTONIC_TIMESTAMP": "9199000000",  "__CURSOR": "s=8465bb8793f04489900425ea995cc0a7;i=4b3;b=0123456789abcdef0123456789abcdef;m=2244d99c0;t=65ed229e0b7c0;x=603cb61fe5ba40c1",  "PRIORITY": "6",  "_SYSTEMD_UNIT": "app-worker.service",  "__REALTIME_TIMESTAMP": "1793106599000000",  "__SEQNUM": "1203",  "__SEQNUM_ID": "8465bb8793f04489900425ea995cc0a7"}

Every value is a string, numbers included ("6"). Use tonumber or compare as strings.

[3] Count per unit. Entries without _SYSTEMD_UNIT, such as kernel messages, need a fallback with // or they turn into blank lines.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=_SYSTEMD_UNIT | jq -r "._SYSTEMD_UNIT // \"(none)\"" | sort | uniq -c | sort -rn | head -5    369 ssh.service    235 cron.service    180 nginx.service    154 systemd-logind.service    102 (none)

[4] Count per priority. 0 is emerg, 3 err, 4 warning, 5 notice, 6 info.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=PRIORITY | jq -r .PRIORITY | sort | uniq -c     11 3     53 4     16 5   1123 6

The total is 1,203, matching step 1. To keep the result as JSON, group inside jq. -s loads everything into memory, so for large windows the sort | uniq -c pipeline is lighter.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=PRIORITY | jq -s "group_by(.PRIORITY) | map({priority: .[0].PRIORITY, n: length})" -c[{"priority":"3","n":11},{"priority":"4","n":53},{"priority":"5","n":16},{"priority":"6","n":1123}]

[5] Combine two conditions. Pick warning and above (-p warning) and see where they came from. Falling back to SYSLOG_IDENTIFIER when there is no unit makes kernel show up separately.

console
$ journalctl --file demo.journal --since "$S" --until "$U" -p warning -o json --output-fields=_SYSTEMD_UNIT,SYSLOG_IDENTIFIER | jq -r "._SYSTEMD_UNIT // .SYSLOG_IDENTIFIER // \"(none)\"" | sort | uniq -c | sort -rn | head -5     34 kernel     22 nginx.service      7 app-worker.service      1 init.scope

34 + 22 + 7 + 1 = 64 matches priorities 3 and 4 in step 4 (11 + 53). -p warning selects that level and everything more severe.

[6] Prepare for values that are not strings. Per the documentation, fields over 4096 bytes come out as null, repeated fields as arrays, and non-printable or non-UTF-8 bytes as arrays of numbers. The test file has one entry of each (TAG twice, a message containing \xff\xfe, a 5,000-byte PAYLOAD).

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | jq -r ".MESSAGE|type" | sort | uniq -c      1 array   1202 string

Find fields that arrived as arrays:

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | jq -r "to_entries[] | select(.value|type==\"array\") | .key" | sort | uniq -c      1 MESSAGE      1 TAG

MESSAGE became an array of numbers because of the non-UTF-8 bytes:

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | jq -c "select(.MESSAGE|type==\"array\") | .MESSAGE"[98,97,100,32,255,254,32,98,121,116,101,115]

An array MESSAGE breaks -r output, so when counting by message text put select(.MESSAGE|type=="string") first.

A field over 4096 bytes keeps its key with a null value. Add --all to get the value itself:

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json | jq -c "select(has(\"PAYLOAD\")) | {MESSAGE, PAYLOAD}"{"MESSAGE":"long field","PAYLOAD":null}
console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --all | jq -r "select(has(\"PAYLOAD\")) | .PAYLOAD | length"5000

[7] Timestamps are microsecond strings. Divide by one million to get a readable time (printed in UTC):

console
$ journalctl --file demo.journal --since "$S" --until "$U" -o json --output-fields=PRIORITY -n 1 | jq -r ".__REALTIME_TIMESTAMP, (.__REALTIME_TIMESTAMP|tonumber/1000000|todate)"17931065990000002026-10-27T13:09:59Z

Counting by message text, such as failed SSH logins, is covered in count failed SSH passwords with journalctl.

Analyze your syslog · journal log in the browser ↗ — nothing is uploaded.

Check it with the JSON · YAML formatter and converter tool ↗ — calculated in your browser.

#journalctl#jq#json#systemd#logs