How to filter invalid jsonlines using jq
When using jsonlines data with jq
, invalid lines can cause parsing errors, e.g.
example.txt
jq: parse error: Expected separator between values at line 61755, column 1954
To filter out invalid lines, you can use the following command:
example.sh
jq -R 'fromjson?'
This will passthrough valid JSON lines and return null
for invalid ones. You can then filter out the null
values using select
. The subsequent command needs to handle null
values correctly, but it won’t see any invalid lines.
Example usage
example.sh
cat data.json | jq -R 'fromjson?' | jq -c '{name: .node?.name?}'
Check out similar posts by category:
JSON
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow