[英]jq - how to filter values in an inner array without knowing the key
I have the following JSON:我有以下 JSON:
{
"ids": {
"sda": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi2"
],
"sdb": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0"
],
"sdb1": [
"lvm-pv-uuid-lvld3A-oA4k-hC19-DXzv-D0Fq-xyME-BwgJid",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-part1"
],
"sdc": [
"lvm-pv-uuid-pWes2W-dgYF-l8hG-La48-9ozH-hPdU-MOkOtf",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi1"
]
}
}
What I want to achieve is to search for .*scsi0$
in the values of the inner array and get sdb
as the result.我想要实现的是在内部数组的值中搜索
.*scsi0$
并获得sdb
作为结果。
Using JSON jq endswith
to filter results:使用JSON jq
endswith
过滤结果:
.ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
Results in:结果是:
"sdb"
Try it here: https://jqplay.org/s/DAhKosXXgiA在这里试试: https ://jqplay.org/s/DAhKosXXgiA
First get .ids
:首先获取
.ids
:
{
"sda": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi2"
],
"sdb": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0"
],
"sdb1": [
"lvm-pv-uuid-lvld3A-oA4k-hC19-DXzv-D0Fq-xyME-BwgJid",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-part1"
],
"sdc": [
"lvm-pv-uuid-pWes2W-dgYF-l8hG-La48-9ozH-hPdU-MOkOtf",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi1"
]
}
...then pipe the results to the to_entries function to convert that to an array of {key, value}
objects, .ids | to_entries
...然后将结果通过管道传输到to_entries函数,以将其转换为
{key, value}
对象数组, .ids | to_entries
.ids | to_entries
: .ids | to_entries
:
[
{
"key": "sda",
"value": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi2"
]
},
{
"key": "sdb",
"value": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0"
]
},
{
"key": "sdb1",
"value": [
"lvm-pv-uuid-lvld3A-oA4k-hC19-DXzv-D0Fq-xyME-BwgJid",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-part1"
]
},
{
"key": "sdc",
"value": [
"lvm-pv-uuid-pWes2W-dgYF-l8hG-La48-9ozH-hPdU-MOkOtf",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi1"
]
}
]
...next stream the list of objects, .ids | to_entries[]
...下一个流对象列表,
.ids | to_entries[]
.ids | to_entries[]
: .ids | to_entries[]
:
{
"key": "sda",
"value": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi2"
]
}
{
"key": "sdb",
"value": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0"
]
}
{
"key": "sdb1",
"value": [
"lvm-pv-uuid-lvld3A-oA4k-hC19-DXzv-D0Fq-xyME-BwgJid",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0-part1"
]
}
{
"key": "sdc",
"value": [
"lvm-pv-uuid-pWes2W-dgYF-l8hG-La48-9ozH-hPdU-MOkOtf",
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi1"
]
}
...and select from a stream of values, .ids | to_entries[] | select(.value[])
...并从值流中选择,
.ids | to_entries[] | select(.value[])
.ids | to_entries[] | select(.value[])
.ids | to_entries[] | select(.value[])
where value endswith "scsi0", select(.value[] | endswith("scsi0"))
: .ids | to_entries[] | select(.value[])
where value endwith "scsi0", select(.value[] | endswith("scsi0"))
:
{
"key": "sdb",
"value": [
"scsi-0QEMU_QEMU_HARDDISK_drive-scsi0"
]
}
...finally, get the key value, .ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
...最后,获取键值
.ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
.ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
.ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
: .ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key
:
"sdb"
Command line:命令行:
jq '.ids | to_entries[] | select(.value[] | endswith("scsi0")) | .key'
Try it here: https://jqplay.org/s/DAhKosXXgiA在这里试试: https ://jqplay.org/s/DAhKosXXgiA
.ids | to_entries[] | select(.value[] | test(".*scsi0$")) | .key
Will print the key if any value in the array matches your regex.如果数组中的任何值与您的正则表达式匹配,将打印密钥。
If none match, there will be no output.如果没有匹配,将没有输出。
to_entries
is uses to easy capture the key of the objects. to_entries
用于轻松捕获对象的键。
select
to filter the values select
以过滤值
.value[] | test(".*scsi0$")
.value[] | test(".*scsi0$")
checks each value to the regex .value[] | test(".*scsi0$")
检查每个值到正则表达式
.key
and we show the key of the result .key
我们显示结果的关键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.