jq - 选择后获取更高级别的密钥
jq - Get a higher level key after a selection
给出如下 JSON:
{
"data": [{
"id": "1a2b3c",
"info": {
"a": {
"number": 0
},
"b": {
"number": 1
},
"c": {
"number": 2
}
}
}]
}
我想 select 一个大于或等于 2 的数字,对于那个 selection 我想 return id
的值和number
。我是这样做的:
$ jq -r '.data[] | .id as $ID | .info[] | select(.number >= 2) | [$ID, .number]' in.json
[
"1a2b3c",
2
]
现在我还想 return 我的 selection 的更高级别密钥,在我的情况下我需要 return c
。我怎样才能做到这一点?
假设您想要在输出中使用字符串 "c"
而不是 2
,这将起作用:
$ jq '.data[] | .id as $ID | .info | to_entries[] | select(.value.number >= 2) | [$ID, .key]' input.json
[
"1a2b3c",
"c"
]
给出如下 JSON:
{
"data": [{
"id": "1a2b3c",
"info": {
"a": {
"number": 0
},
"b": {
"number": 1
},
"c": {
"number": 2
}
}
}]
}
我想 select 一个大于或等于 2 的数字,对于那个 selection 我想 return id
的值和number
。我是这样做的:
$ jq -r '.data[] | .id as $ID | .info[] | select(.number >= 2) | [$ID, .number]' in.json
[
"1a2b3c",
2
]
现在我还想 return 我的 selection 的更高级别密钥,在我的情况下我需要 return c
。我怎样才能做到这一点?
假设您想要在输出中使用字符串 "c"
而不是 2
,这将起作用:
$ jq '.data[] | .id as $ID | .info | to_entries[] | select(.value.number >= 2) | [$ID, .key]' input.json
[
"1a2b3c",
"c"
]