如何很好地删除jq中的空数组 [英] How to nicely remove empty array in jq
问题描述
我正在阅读O'Reilly的新文章《命令行中的数据科学》,并且使用jq遇到了麻烦.我有一些JSON(从NYTimes Articles API返回),我正在用jq进行解析,如下所示:
I am reading O'Reilly's new "Data Science at the Command Line" and running into trouble using jq. I have some JSON (returned from the NYTimes Articles API) I am parsing with jq like so:
jq -c \
'[.response.docs[] | {date: .pub_date, type: .document_type, title: .headline.main }]' \
< myjsonfile.json
因此,我正在寻找"response":"docs"(这是一个数组),然后将该数组中的每个项目与"pub_type"等进行匹配,将其重命名,依此类推.这很好用,但是在末尾添加了一个空数组:
So, I am looking for "response":"docs" (which is an array) and then matching every item in that array with "pub_type" etc., renaming it, and so on. This works great, but it adds an empty array at the end:
[{"date":"2009-01-02T00:00:00Z","type":"article","title":"SPARE TIMES: AROUND TOWN"},
{"date":"2009-01-02T00:00:00Z","type":"article","title":"Catskill Home Prices: How Low Will They Go?"},
{"date":"2009-01-01T00:00:00Z","type":"article","title":"Ominous Cutbacks At Chanel"}]
[]
如何摆脱空数组?我现在的解决方案是将输出通过管道返回到jq,但这感觉确实不是很理想.这样就可以了:
How do I get rid of the empty array? My solution right now is to pipe the output back into jq, but that feels really suboptimal. So this works:
jq -c \
'[.response.docs[] | {date: .pub_date, type: .document_type, title: .headline.main }]' | \
< myjsonfile.json |
jq 'if length > 0 then . else empty end'
但这感觉很难看.有更好的方法吗?
But that feels ugly. Is there a nicer way of doing this?
推荐答案
使用select
过滤器,其中长度> 0.
Use the select
filter where length > 0.
select(length > 0)
这篇关于如何很好地删除jq中的空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!