[英]How to search json for a string and output a parent value using jq?
Trying to pull a value out with jq, but didn't seem as easy as i originally thought.试图用 jq 提取一个值,但似乎并不像我最初想象的那么容易。
I have a variable of CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
.我有一个变量CAR="baga6e~tlwdcmli__QmbHKa~G65fMXzh.car"
。
How can i use this variable to return the parent "piece_cid"
?如何使用此变量返回父"piece_cid"
?
Example: Using a bash varible of $CAR
with a value of baga6e~tlwdcmli__QmbHKa~G65fMXzh.car
, id be able to get the results saved to $PIECE_CID
with a value of baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
示例:使用值为baga6e~tlwdcmli__QmbHKa~G65fMXzh.car
的 bash 变量$CAR
,我能够将结果保存到$PIECE_CID
,值为baga6ea4seaqa24ucggxkliw4la4tkgvhevv3dacavovghsmnnscclt4tlwdcmli
I tried some commands and been googling for about 2 hours but nothing has came up for what im seeking.我尝试了一些命令并在谷歌上搜索了大约 2 个小时,但没有找到我想要的东西。 Completely new to jq.对 jq 来说是全新的。 Hopefully someone can push me in right direction?希望有人能把我推向正确的方向?
{
"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"
}
]
}
Depending on what level you can expect your search value, here's a quite narrow solution using all keys from the example: First, iterate over the .response
array, then keep only those items for which any
of the values in .sources[].sample_retrieve_cmd
contains
the given string, then output the value of .piece_cid
as raw text using -r
.根据您可以期望的搜索值的级别,这是一个使用示例中所有键的非常狭窄的解决方案:首先,遍历.response
数组,然后仅保留.sources[].sample_retrieve_cmd
中的any
值的那些项目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
Edit: To save the output into a variable just wrap the jq
call as command substitution $()
and assign the result to your variable:编辑:要将输出保存到变量中,只需将jq
调用包装为命令替换$()
并将结果分配给您的变量:
PIECE_CID="$(jq -r --arg …)"
But keep in mind that (theoretically) more than one match could be present, which the solution from above would print out linewise (giving you a newline-delimited list of results in your bash variable).但请记住,(理论上)可能存在多个匹配项,上面的解决方案将逐行打印出来(在 bash 变量中为您提供一个以换行符分隔的结果列表)。
Note: Avoid using uppercase variable names in Bash.注意:避免在 Bash 中使用大写的变量名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.