![](/img/trans.png)
[英]How do you get a custom tag value in JQuery when you have multiple buttons?
[英]jq: print key and value for each entry in an object when you have multiple values
{
"host1": [
{"ip": "10.1.2.3"},
{"ip": "10.2.3.4"},
{"ip": "10.3.4.5"},
],
"host2": [
{"ip":"10.1.2.2"},
{"ip":"10.4.5.6"},
],
"host3": [
{"ip":"10.1.18.1"}
]
}
我从另一个问题中借用了大部分结构,但改变了它,因为现在一个条目可以有多个值。
我希望 jq 的 output 是
host1 10.1.2.3
host1 10.2.3.4
host1 10.3.4.5
host2 10.1.2.2
host2 10.4.5.6
host3 10.1.18.1
host1 和 host2 并为每个值打印。
我的尝试是:
jq -r 'to_entries[] | [.key, .value[].ip] | @tsv' file.json
但是那个 output 是
host1 10.1.2.3 10.2.3.4 10.3.4.5
host2 10.1.2.2 10.4.5.6
host3 10.1.18.1
但我希望它们在不同的行上。
谢谢
将 ips 单独保存到一个变量中以在数组中引用它:
jq -r 'to_entries[] | .value[].ip as $ip | [.key, $ip] | @tsv'
host1 10.1.2.3
host1 10.2.3.4
host1 10.3.4.5
host2 10.1.2.2
host2 10.4.5.6
host3 10.1.18.1
除了to_entries
,您还可以使用keys_unsorted
来访问密钥:
jq -r 'keys_unsorted[] as $key | .[$key][] | [$key, .ip] | @tsv'
host1 10.1.2.3
host1 10.2.3.4
host1 10.3.4.5
host2 10.1.2.2
host2 10.4.5.6
host3 10.1.18.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.