On , I learnt ...

How to use jq to only parse valid JSON lines

Suppose you have a stream of text comprising JSON and non-JSON lines — e.g:

$ cat example.txt
This is plain text
{"message": "This is JSON"}

and you want to pretty-print the JSON lines to make the stream more readable.

Piping this into jq fails because the first line is not valid JSON:

$ cat example.txt | jq
parse error: Invalid numeric literal at line 1, column 5

But you use jq’s fromjson filter in a try/catch block to format the JSON lines and ignore the others:

$ cat example.txt | jq -R '. as $line | try (fromjson) catch $line'
"This is plain text"
{
  "message": "This is JSON"
}

This works well when running uWSGI with JSON logging enabled, where the initial log lines are not JSON formatted.