mfa*_*aiz 3 shell json try-catch jq
我试图根据同一对象中另一个键的“包含”值来获取值
我已经尝试了一个代码,它可以工作并输出我想要的结果,但是 JSON 中的某些对象没有这个键,因此我得到:
jq: error (at <stdin>:1): null (null) and string ("BBC") cannot have their containment checked
或者这个错误的原因是其他键中的数组,我不确定
使用:
jq '.entries[] | select(.icon | contains("BBC")) | .uuid'
我希望找到的结果的 UUID 没有错误,并将其作为变量存储在 shell 中
"174501xxxxxxxxxxxxxe6342a03"
通过管道传输的输入文件
{
"entries":[
{
"uuid":"174501xxxxxxxxxxxxxe6342a03",
"enabled":true,
"autoname":true,
"name":"BBC",
"number":0,
"icon":"file:///logos/BBC.png",
"icon_public_url":"imagecache/1097",
"epgauto":true,
"epggrab":[ ],
"dvr_pre_time":0,
"dvr_pst_time":0,
"epg_running":-1,
"services":[ ],
"tags":[ ],
"bouquet":""
},
{
"uuid":"174501xxxxxxxxxxxxxe6342a04",
"enabled":true,
"autoname":true,
"name":"ABC",
"number":0,
"icon_public_url":"imagecache/1098",
"epgauto":true,
"epggrab":[ ],
"dvr_pre_time":0,
"dvr_pst_time":0,
"epg_running":-1,
"services":[ ],
"tags":[ ],
"bouquet":""
}...
Run Code Online (Sandbox Code Playgroud)
您只能预先选择具有 的icon
键的对象has("icon")
。
来自jq 1.5 手册:
has(key)
内置函数
has
返回输入对象是否具有给定键,或者输入数组是否在给定索引处具有元素。
jq '.entries[] | select(has("icon")) | select(.icon | contains("BBC")).uuid' file
Run Code Online (Sandbox Code Playgroud)
"icon":null
但是,如果您的对象有这种情况,它会输出错误,您可以使用:
jq '.entries[] | select(.icon != null) | select(.icon | contains("BBC")).uuid' file
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4427 次 |
最近记录: |