如何使用JQ将JSON对象生成数组?
已对执行了以下操作: 输入如何使用JQ将JSON对象生成数组?,json,linux,bash,unix,jq,Json,Linux,Bash,Unix,Jq,已对执行了以下操作: 输入 jq'带有_条目(选择([.key]|内部([“adaway”、“adguard”、“disconnect”、“yoyo”))”adblock.sources 输出 { “阿达威”:{ “url”:”https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt", “规则”:“/^127\\.0\\.0\\.1[:space:][]+([:alnum:][-]{1,63}\\)+
jq'带有_条目(选择([.key]|内部([“adaway”、“adguard”、“disconnect”、“yoyo”))”adblock.sources
输出
{
“阿达威”:{
“url”:”https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt",
“规则”:“/^127\\.0\\.0\\.1[:space:][]+([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][].$)/{print tolower($2)}”,
“大小”:“S”,
“焦点”:“移动”,
“描述URL”:https://github.com/AdAway/adaway.github.io"
},
“adguard”:{
“url”:”https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt",
“规则”:“BEGIN{FS=\”[/^^\\\\r]\“}/^\\\\\\\\\\\\([[:alnum:\\\\\]{1,63}\\\)+[:alpha:]+[\\/\\\\\\\\r]+$/{print tolower($3)}”,
“尺寸”:“L”,
“焦点”:“一般”,
“描述URL”:https://adguard.com"
},
“断开连接”:{
“url”:”https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt",
“规则”:“/^([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][]}$)/{print tolower($1)}”,
“大小”:“S”,
“焦点”:“一般”,
“描述URL”:https://disconnect.me"
},
“yoyo”:{
“url”:”https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=0&mimetype=plaintext",
“规则”:“/^([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][]}$)/{print tolower($1)}”,
“大小”:“S”,
“焦点”:“一般”,
“描述URL”:https://pgl.yoyo.org"
}
}
我希望它是一个数组,并尝试了一些建议,比如将它管道化到
jq-s.
。这些尝试只产生一个元素数组。设置此格式的最佳方式是什么?我高度怀疑from_条目
和to_条目
必须以某种方式合并
预期产出
[
{
“密钥”:“adaway”,
“url”:”https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt",
“规则”:“/^127\\.0\\.0\\.1[:space:][]+([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][].$)/{print tolower($2)}”,
“大小”:“S”,
“焦点”:“移动”,
“描述URL”:https://github.com/AdAway/adaway.github.io"
},
{
“密钥”:“adguard”,
“url”:”https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt",
“规则”:“BEGIN{FS=\”[/^^\\\\r]\“}/^\\\\\\\\\\\\([[:alnum:\\\\\]{1,63}\\\)+[:alpha:]+[\\/\\\\\\\\r]+$/{print tolower($3)}”,
“尺寸”:“L”,
“焦点”:“一般”,
“描述URL”:https://adguard.com"
},
{
“键”:“断开连接”,
“url”:”https://s3.amazonaws.com/lists.disconnect.me/simple_malvertising.txt",
“规则”:“/^([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][]}$)/{print tolower($1)}”,
“大小”:“S”,
“焦点”:“一般”,
“描述URL”:https://disconnect.me"
},
{
“钥匙”:“yoyo”,
“url”:”https://pgl.yoyo.org/adservers/serverlist.php?hostformat=nohtml&showintro=0&mimetype=plaintext",
“规则”:“/^([:alnum:][-]{1,63}\\)+[:alpha:][+([:space:][]}$)/{print tolower($1)}”,
“大小”:“S”,
“焦点”:“一般”,
“描述URL”:https://pgl.yoyo.org"
}
]
正如评论中所暗示的,有几种方法可以解释最初提出的问题,但三种主要解释可以通过在jq过滤器中添加以下内容之一来实现:
| [keys_unsorted[] as $key | {$key} + .[$key] ]
或
或:
更新:第一个是与显示的预期输出相对应的
{$key}
是对象数组的缩写?你能举一个预期输出的例子吗?为了清晰起见,我添加了一些预期输出
| map(.)
| [keys_unsorted[] as $key | { $key): .[$key] } ]