如何使用 jq 搜索 json 字符串并输出父值?
How to search json for a string and output a parent value using jq?
试图用 jq 提取一个值,但似乎并不像我最初想象的那么容易。
我有一个变量CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
。
如何将此变量用于 return 父 "piece_cid"
?
示例: 使用 $CAR
的 bash 变量,值为 baga6e~tlwdcmli__QmbHKa~G65fMXzh.car
,id 能够将结果保存到$PIECE_CID
值为 baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
我尝试了一些命令并在谷歌上搜索了大约 2 个小时,但没有找到我正在寻找的东西。对 jq 来说是全新的。希望有人能把我推向正确的方向?
{
"response_code": 200,
"response_entries": 2,
"response": [
{
"piece_cid": "baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli",
"dataset": "genome-ark",
"padded_piece_size": 34359738368,
"payload_cids": [
"QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh"
],
"sources": [
{
"source_type": "Filecoin",
"provider_id": "f0402371",
"deal_id": 1928414,
"original_payload_cid": "QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh",
"deal_expiration": "2022-05-22T21:14:30Z",
"is_filplus": false,
"sector_id": null,
"sector_expires": null,
"sample_retrieve_cmd": "lotus client retrieve --provider f0402371 --maxPrice 0 --allow-local --car 'QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh' $(pwd)/baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
}
],
"sample_request_cmd": "echo curl -sLH \"Authorization: $( ./fil-spid.bash f01826669 )\" https://api.evergreen.filecoin.io/request_piece/baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli | sh"
},
{
"piece_cid": "baga6ea4seaqa2acwhwril5pm6n4muqlsrdkk27cgrqowz67himgbcwc3jhitina",
"dataset": "genome-ark",
"padded_piece_size": 34359738368,
"payload_cids": [
"QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42"
],
"sources": [
{
"source_type": "Filecoin",
"provider_id": "f0402371",
"deal_id": 1927852,
"original_payload_cid": "QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42",
"deal_expiration": "2022-05-22T20:11:00Z",
"is_filplus": false,
"sector_id": null,
"sector_expires": null,
"sample_retrieve_cmd": "lotus client retrieve --provider f0402371 --maxPrice 0 --allow-local --car 'QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42' $(pwd)/baga6e~3jhitina__QmSJc7~oCoCeW42.car"
}
],
"sample_request_cmd": "echo curl -sLH \"Authorization: $( ./fil-spid.bash f01826669 )\" https://api.evergreen.filecoin.io/request_piece/baga6ea4seaqa2acwhwril5pm6n4muqlsrdkk27cgrqowz67himgbcwc3jhitina | sh"
}
]
}
根据您期望的搜索值级别,这里有一个使用示例中所有键的非常狭窄的解决方案:首先,遍历 .response
数组,然后仅保留 [=14] =] .sources[].sample_retrieve_cmd
contains
给定字符串中的值,然后使用 -r
.
将 .piece_cid
的值输出为原始文本
CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
jq -r --arg car "$CAR" '
.response[]
| select(any(.sources[].sample_retrieve_cmd; contains($car)))
| .piece_cid
'
baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
编辑:要将输出保存到变量中,只需将 jq
调用包装为命令替换 $()
并将结果分配给您的变量:
PIECE_CID="$(jq -r --arg …)"
但请记住(理论上)可能存在多个匹配项,上面的解决方案将逐行打印出来(在 bash 变量中为您提供 newline-delimited 结果列表).
注意:避免在 Bash.
中使用大写变量名
试图用 jq 提取一个值,但似乎并不像我最初想象的那么容易。
我有一个变量CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
。
如何将此变量用于 return 父 "piece_cid"
?
示例: 使用 $CAR
的 bash 变量,值为 baga6e~tlwdcmli__QmbHKa~G65fMXzh.car
,id 能够将结果保存到$PIECE_CID
值为 baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
我尝试了一些命令并在谷歌上搜索了大约 2 个小时,但没有找到我正在寻找的东西。对 jq 来说是全新的。希望有人能把我推向正确的方向?
{
"response_code": 200,
"response_entries": 2,
"response": [
{
"piece_cid": "baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli",
"dataset": "genome-ark",
"padded_piece_size": 34359738368,
"payload_cids": [
"QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh"
],
"sources": [
{
"source_type": "Filecoin",
"provider_id": "f0402371",
"deal_id": 1928414,
"original_payload_cid": "QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh",
"deal_expiration": "2022-05-22T21:14:30Z",
"is_filplus": false,
"sector_id": null,
"sector_expires": null,
"sample_retrieve_cmd": "lotus client retrieve --provider f0402371 --maxPrice 0 --allow-local --car 'QmbHKaq6z321PqEG1BVLEAMpapq6YjkhdXpcs3G65fMXzh' $(pwd)/baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
}
],
"sample_request_cmd": "echo curl -sLH \"Authorization: $( ./fil-spid.bash f01826669 )\" https://api.evergreen.filecoin.io/request_piece/baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli | sh"
},
{
"piece_cid": "baga6ea4seaqa2acwhwril5pm6n4muqlsrdkk27cgrqowz67himgbcwc3jhitina",
"dataset": "genome-ark",
"padded_piece_size": 34359738368,
"payload_cids": [
"QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42"
],
"sources": [
{
"source_type": "Filecoin",
"provider_id": "f0402371",
"deal_id": 1927852,
"original_payload_cid": "QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42",
"deal_expiration": "2022-05-22T20:11:00Z",
"is_filplus": false,
"sector_id": null,
"sector_expires": null,
"sample_retrieve_cmd": "lotus client retrieve --provider f0402371 --maxPrice 0 --allow-local --car 'QmSJc7g8sdUZKt2MqSDnrd1DXxxHow6XwKQZLcoCoCeW42' $(pwd)/baga6e~3jhitina__QmSJc7~oCoCeW42.car"
}
],
"sample_request_cmd": "echo curl -sLH \"Authorization: $( ./fil-spid.bash f01826669 )\" https://api.evergreen.filecoin.io/request_piece/baga6ea4seaqa2acwhwril5pm6n4muqlsrdkk27cgrqowz67himgbcwc3jhitina | sh"
}
]
}
根据您期望的搜索值级别,这里有一个使用示例中所有键的非常狭窄的解决方案:首先,遍历 .response
数组,然后仅保留 [=14] =] .sources[].sample_retrieve_cmd
contains
给定字符串中的值,然后使用 -r
.
.piece_cid
的值输出为原始文本
CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
jq -r --arg car "$CAR" '
.response[]
| select(any(.sources[].sample_retrieve_cmd; contains($car)))
| .piece_cid
'
baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
编辑:要将输出保存到变量中,只需将 jq
调用包装为命令替换 $()
并将结果分配给您的变量:
PIECE_CID="$(jq -r --arg …)"
但请记住(理论上)可能存在多个匹配项,上面的解决方案将逐行打印出来(在 bash 变量中为您提供 newline-delimited 结果列表).
注意:避免在 Bash.
中使用大写变量名