如何使用 jq 过滤无效的 jsonlines
使用 jq 处理 jsonlines 数据时,无效行可能导致解析错误,例如
e.g
jq: parse error: Expected separator between values at line 61755, column 1954要过滤掉无效行,你可以使用以下命令:
filter_invalid_jsonlines.sh
jq -R 'fromjson?'这将传递有效的 JSON 行并为无效行返回 null。然后你可以使用 select 过滤掉 null 值。后续命令需要正确处理 null 值,但不会看到任何无效行。
示例用法
filter_and_process_jsonlines.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