我正试图从它使用的源列表中检索火狐的黑名单主机,以便我可以将它用于另一个浏览器(Qutebrowser)。
在解析JSON方面,我在jq方面相当成功。
#!/bin/sh
for term in Advertising Content Social Analytics Fingerprinting Cryptomining Disconnect; do
jq ".categories.$term[][][][]" services.json
done但是,一些类别中的几个最深对象(它们总是处于相同的嵌套级别)包含了打破jq的额外信息,如下面的"performance": "true":
{
"categories": {
...
"Cryptomining": [
{
"a.js": {
"http://zymerget.bid": [
"alflying.date",
"alflying.win",
...
"zymerget.faith"
],
"performance": "true"
}
},
{
"CashBeet": {
"http://cashbeet.com": [
"cashbeet.com",
"serv1swork.com"
]
}
},
...因此,例如,当循环转到jq ".categories.Cryptomining[][][][]" services.json时,它会引发一个错误并停止处理类别:
"alflying.date"
"alflying.win"
...
"zymerget.faith"
jq: error (at servicesN.json:11167): Cannot iterate over string ("true")有没有办法用jq忽略那些非数组属性?另外,请告诉我是否可以放弃for循环,在一个jq中完成整个过程(因为目前,正如上面所示,我列出了for循环中的所有类别)。
发布于 2020-06-21 02:01:46
发布于 2020-06-21 20:46:29
给定的
{
"categories": {
"Cryptomining": [
{
"a.js": {
"http://zymerget.bid": [
"alflying.date",
"alflying.win",
"zymerget.faith"
],
"performance": "true"
}
},
{
"CashBeet": {
"http://cashbeet.com": [
"cashbeet.com",
"serv1swork.com"
]
}
}
]
}
}作为嵌套路径的替代方案,您可以使用递归下降:
.. | strings它产生:
"alflying.date"
"alflying.win"
"zymerget.faith"
"true"
"cashbeet.com"
"serv1swork.com"若要排除"true“,可以将其设置为布尔值,也可以将不包含.的字符串排除在外:
.. | strings | select(contains("."))返回:
"alflying.date"
"alflying.win"
"zymerget.faith"
"cashbeet.com"
"serv1swork.com"https://stackoverflow.com/questions/62493191
复制相似问题