json - 如何忽略 jq 中损坏的 JSON 行?
问题描述
用于jq
处理日志文件时,某些行可能会中断,因此jq
会引发错误并停止处理。
例如完整的日志:
{"level":"debug","time":"2021-09-24T19:42:47.140+0800","message":"sent send binary to ws server1","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.305+0800","message":"sent send binary to ws server2","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.469+0800","message":"sent send binary to ws server3","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.499+0800","message":"sent send binary to ws server4","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.581+0800","message":"sent send binary to ws server5","pid":41491,"cid":"32likw","num":1,"count":5120}
jq 处理得很好:
< snippet1.json jq -C -r '.message'
sent send binary to ws server1
sent send binary to ws server2
sent send binary to ws server3
sent send binary to ws server4
sent send binary to ws server5
损坏的(第 3 行的最后一部分丢失):
{"level":"debug","time":"2021-09-24T19:42:47.140+0800","message":"sent send binary to ws server1","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.305+0800","message":"sent send binary to ws server2","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.469+0800","message":"sent send binary to ws server3","pi
{"level":"debug","time":"2021-09-24T19:42:47.499+0800","message":"sent send binary to ws server4","pid":41491,"cid":"32likw","num":1,"count":5120}
{"level":"debug","time":"2021-09-24T19:42:47.581+0800","message":"sent send binary to ws server5","pid":41491,"cid":"32likw","num":1,"count":5120}
jq
停在虚线处:
< snippet2.json jq -C -r '.message'
sent send binary to ws server1
sent send binary to ws server2
parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 4, column 2
我希望jq
可以忽略第 3 行并继续,就像这样:
< snippet2.json jq -C -r '.message'
sent send binary to ws server1
sent send binary to ws server2
sent send binary to ws server4
sent send binary to ws server5
我尝试使用另一篇文章-R
中提到的,它对这种情况没有帮助。
< snippet2.json jq -C -R -r '.message'
jq: error (at <stdin>:1): Cannot index string with string "message"
jq: error (at <stdin>:2): Cannot index string with string "message"
jq: error (at <stdin>:3): Cannot index string with string "message"
jq: error (at <stdin>:4): Cannot index string with string "message"
jq: error (at <stdin>:5): Cannot index string with string "message"
您能否让我知道是否有任何解决方案/技能可以忽略/跳过/抑制此类错误并获得其余结果?
解决方案
要跳过虚线,您可以使用:
jq -Rr 'fromjson? | .message'
如果您想对它们做其他事情,可以从以下内容开始:
jq -R '. as $line | try fromjson catch $line'
有关其他选项,请参阅:
: 有没有办法让 jq 在输入文件中遇到错误后继续运行?jq 可以处理损坏的 JSON 吗?
在jq 常见问题解答中。
推荐阅读
- swift - Cocoa:如何在 Swift 中获取广播 IP 地址?
- c# - 如何在 ASP 网络核心 Web API 上正确地将列表转换为 IQueryable
- python - 索引错误,似乎无法弄清楚这一点
- kubernetes - 查询以查找集群中 CPU 和内存的不同组合
- php - PHP:从序列化数组中删除整个数组的字符串计数
- django - Django ORM:使用数据透视表/中间表
- python - 如何使用此正则表达式模式提取子字符串?它给出了一个 ValueError: too many values to unpack (expected 1)
- python - 多处理实例之间的 Numpy 数组共享
- flutter - 我可以将 flutter_form_builder_package 与无状态小部件和提供程序一起使用吗?
- python - 如何删除不需要的空白