[英]How is a JSON object made into an array using JQ?
The following has been performed on this JSON file : 对此 JSON 文件执行了以下操作:
INPUT输入
jq 'with_entries(select([.key] | inside(["adaway", "adguard", "disconnect", "yoyo"])))' adblock.sources
OUTPUT OUTPUT
{
"adaway": {
"url": "https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt",
"rule": "/^127\\.0\\.0\\.1[[:space:]]+([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($2)}",
"size": "S",
"focus": "mobile",
"descurl": "https://github.com/AdAway/adaway.github.io"
},
"adguard": {
"url": "https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt",
"rule": "BEGIN{FS=\"[/|^|\\r]\"}/^\\|\\|([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+[\\/\\^\\r]+$/{print tolower($3)}",
"size": "L",
"focus": "general",
"descurl": "https://adguard.com"
},
"disconnect": {
"url": "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt",
"rule": "/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($1)}",
"size": "S",
"focus": "general",
"descurl": "https://disconnect.me"
},
"yoyo": {
"url": "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=0&mimetype=plaintext",
"rule": "/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($1)}",
"size": "S",
"focus": "general",
"descurl": "https://pgl.yoyo.org"
}
}
I'd like to have it be an array, and have tried recommendations like piping it into jq -s '.'
我想让它成为一个数组,并尝试过一些建议,比如将它输送到
jq -s '.'
. . These attempts only yield a single-element array.
这些尝试只产生一个单元素数组。 What would be the best way to format this?
格式化它的最佳方法是什么? I highly suspect
from_entries
and to_entries
have to be incorporated somehow.我高度怀疑必须以某种方式合并
from_entries
和to_entries
。
EXPECTED OUTPUT预期 OUTPUT
[
{
"key": "adaway",
"url": "https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt",
"rule": "/^127\\.0\\.0\\.1[[:space:]]+([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($2)}",
"size": "S",
"focus": "mobile",
"descurl": "https://github.com/AdAway/adaway.github.io"
},
{
"key": "adguard",
"url": "https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt",
"rule": "BEGIN{FS=\"[/|^|\\r]\"}/^\\|\\|([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+[\\/\\^\\r]+$/{print tolower($3)}",
"size": "L",
"focus": "general",
"descurl": "https://adguard.com"
},
{
"key": "disconnect",
"url": "https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt",
"rule": "/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($1)}",
"size": "S",
"focus": "general",
"descurl": "https://disconnect.me"
},
{
"key": "yoyo",
"url": "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=0&mimetype=plaintext",
"rule": "/^([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+([[:space:]]|$)/{print tolower($1)}",
"size": "S",
"focus": "general",
"descurl": "https://pgl.yoyo.org"
}
]
As implied in a comment, there are several ways of interpreting the question as originally asked, but the three main interpretations could be realized by adding one of the following to your jq filter:正如评论中所暗示的那样,有几种方法可以解释最初提出的问题,但是可以通过将以下之一添加到 jq 过滤器来实现三种主要解释:
| [keys_unsorted[] as $key | {$key} + .[$key] ]
or或者
| map(.)
or:或者:
| [keys_unsorted[] as $key | { $key): .[$key] } ]
Update: The first of these is the one corresponding to the shown expected output.更新:其中第一个是对应于所示预期 output 的那个。
{$key}
is shorthand for {"key": $key}
. {$key}
是{"key": $key}
的简写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.