The jsoncompleter
repository includes both the jsoncompleter
package and the jr
CLI tool, providing a simple way to complete truncated JSON
strings.
Too many times, I encountered issues with truncated JSON
entries in JSONL
(JSON
Lines) log dumps, making debugging with tools like jq
and others frustrating.
That's where jr
comes in, completing truncated JSON
strings to make log analysis easier.
Truncated JSON
data might still contain useful information, jr
ensures that no data is discarded due to truncation.
It marks missing parts with placeholders, making it easy to track what's missing.
go get github.com/ladzaretti/jsoncompleter
go install github.com/ladzaretti/jsoncompleter/cmd/jr@latest
Precompiled binaries for various architectures are available on the release page.
$ jr -h
jr - commandline tool for completing truncated JSON lines.
Usage: jr [options] [strings...]
-m, --mark Enable marking of truncated JSON lines
-p, --placeholder Set a custom placeholder for marking truncation
-s, --skip-invalid Skip invalid JSON strings from output
-d, --debug Print the position or line number of skipped invalid JSON strings to stderr
Without jr
This demonstrates the error when parsing a truncated JSON string, where data after the error is not processed.
This JSONL input has 3 lines: the first and last are valid, the second is truncated.
$ echo -e '{"foo":"bar"}\n{"baz":\n{"qux":null}' | jq -c
{"foo":"bar"}
jq: parse error: Unfinished JSON term at EOF at line 4, column 0
With jr
This will complete the truncated JSON and allow the parsing to proceed.
$ echo -e '{"foo":"bar"}\n{"baz":\n{"qux":null}' | jr -m | jq -c
{"foo":"bar"}
{"baz":"","__TRUNCATION_MARKER__":""}
{"qux":null}