[英]Looping through a json file using jq
Related to: Getting data from json using jq when key is numerical string but different as I have the numerical key stored in a variable. 相关信息: 当键为数字字符串但使用数字键存储在变量中时,使用jq从json获取数据 。 See the example below.
请参见下面的示例。
File temp.json
: 文件
temp.json
:
{
"bccc26321e360ae5fde94aac81eef7c7270bbfd90de0787d0e5b45be4b21ce53": {
"size": 189,
"fee": 0.00000678,
"modifiedfee": 0.00000678,
"time": 1535906461,
"height": 539665
},
"43906c7227610cd58a1c95714e4f79cd46e0d98ae3f4214f1b2cf325b628b70e": {
"size": 256,
"fee": 0.00008328,
"modifiedfee": 0.00008328,
"time": 1535906461,
"height": 539665
}
}
Attempts to index with a variable: 尝试使用变量建立索引:
#get second key 43906c7227610cd58a1c95714e4f79cd46e0d98ae3f4214f1b2cf325b628b70e
>txid=$(cat temp.json | jq 'keys' | jq .[1] | tr -d '"')
>cat temp.json | jq .$txid
jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.43906c7227610cd58a1c95714e4f79cd46e0d98ae3f4214f1b2cf325b628b70e
jq: 1 compile error
>cat temp.json | jq '.$txid'
q: error: syntax error, unexpected '$' (Unix shell quoting issues?) at <top-level>, line 1:
.$txid
jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:
.$txid
jq: 2 compile errors
>cat temp.json | jq '."$txid"'
null
The desired output is simply 所需的输出很简单
>cat temp.json | jq '."43906c7227610cd58a1c95714e4f79cd46e0d98ae3f4214f1b2cf325b628b70e"'
{
"size": 256,
"fee": 0.00008328,
"modifiedfee": 0.00008328,
"time": 1535906461,
"height": 539665
}
If what you want is ."xxx"
as argument, then use ".\\"$txid\\""
. 如果您想要的是
."xxx"
作为参数,则使用".\\"$txid\\""
。 If you drop the tr -d '"'
, ".$txid"
may even be enough. 如果删除
tr -d '"'
, ".$txid"
可能就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.