jq:过滤数组并投影其他字段
jq: filter array and project other field
这是我的文档:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": [
{
"system": {
"value": "urn:oid:1.3.6.1.4.1.19126.3"
},
"value": {
"value": "Y3454867M"
}
},
{
"system": {
"value": "urn:oid:2.16.724.4.9.10.2"
},
"value": {
"value": "108505134"
}
}
]
}
]
我只需要选择 .identifier[where .system.value == "urn:oid:1.3.6.1.4.1.19126.3"]
和项目 .identifier.value.value
。
期望的输出:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]
我一直在玩 map
和 select
,但我不太清楚什么是正确的获取方式。
有什么想法吗?
使用好的 ol' select 工具就可以赚钱,因为您需要来自任意索引的数据。在打开通过管道传输到我的 select.
的内部数组之前,我摸索了一下
jq -r '.[] | [{id: .id, identifier: .identifier | .[] | select(.system.value | contains("urn:oid:1.3.6.1.4.1.19126.3")) | .value.value }]'
我自己还是 jq 的新手,欢迎任何反馈。
此方法使用 first
获取第一个结果,以防有多个数组项符合条件。
jq --arg v "urn:oid:1.3.6.1.4.1.19126.3" '
map(.identifier |= first(.[] | select(.system.value == $v).value.value))
'
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]
这是我的文档:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": [
{
"system": {
"value": "urn:oid:1.3.6.1.4.1.19126.3"
},
"value": {
"value": "Y3454867M"
}
},
{
"system": {
"value": "urn:oid:2.16.724.4.9.10.2"
},
"value": {
"value": "108505134"
}
}
]
}
]
我只需要选择 .identifier[where .system.value == "urn:oid:1.3.6.1.4.1.19126.3"]
和项目 .identifier.value.value
。
期望的输出:
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]
我一直在玩 map
和 select
,但我不太清楚什么是正确的获取方式。
有什么想法吗?
使用好的 ol' select 工具就可以赚钱,因为您需要来自任意索引的数据。在打开通过管道传输到我的 select.
的内部数组之前,我摸索了一下jq -r '.[] | [{id: .id, identifier: .identifier | .[] | select(.system.value | contains("urn:oid:1.3.6.1.4.1.19126.3")) | .value.value }]'
我自己还是 jq 的新手,欢迎任何反馈。
此方法使用 first
获取第一个结果,以防有多个数组项符合条件。
jq --arg v "urn:oid:1.3.6.1.4.1.19126.3" '
map(.identifier |= first(.[] | select(.system.value == $v).value.value))
'
[
{
"id": "9f0e27fe-3b8f-4857-8e1d-e57e7a3f4c31",
"identifier": "Y3454867M"
}
]