Ignore Unparseable JSON with jq
Assuming that each log entry is exactly one line, you can use the -R
or --raw-input
option to tell jq to leave the lines unparsed, after which you can prepend fromjson? |
to your filter to make jq try to parse each line as JSON and throw away the ones that error.
I have log stream where some messages are in json format. I want to pipe the json messages through jq, and just echo the rest.
The json messages are on a single line.
Solution: use grep and tee to split the lines in two streams, those starting with "^{" pipe through jq and the rest just echo to terminal.
kubectl logs -f web-svjkn | tee >(grep -v "^{") | grep "^{" | jq .
or
cat logs | tee >(grep -v "^{") | grep "^{" | jq .
Explanation: tee generates 2nd stream, and grep -v prints non json info, 2nd grep only pipes what looks like json opening bracket to jq.