需要在一个数组中搜索json中的值,并使用jq从另一个数组提供输出

需要在一个数组中搜索json中的值,并使用jq从另一个数组提供输出,json,jq,Json,Jq,我有一个下面的json模式 { "a": [{ "metadata":{ "name":"raj"}, "status":[{ "state": { "sleeping": { "started":"2020-05-11T14:49:04Z", "ID":"1002"} }},{ "state": { "sleeping": { "started":"2020-05-11T14:49:04Z", "ID":"1002"} }}], "phase":"slee

我有一个下面的json模式

{
"a": [{
"metadata":{ "name":"raj"},
"status":[{
"state":
{  "sleeping":
  { "started":"2020-05-11T14:49:04Z",
    "ID":"1002"}
 }},{
"state": 
 { "sleeping":
   { "started":"2020-05-11T14:49:04Z",
     "ID":"1002"}
 }}],
"phase":"sleeping"},

{
"metadata":{ "name":"tom"},
"status":[{
"state":
{  "sleeping":
  { "started":"2020-05-11T14:49:04Z",
    "ID":"1002"}
 }},{
"state": 
 { "sleeping":
   { "started":"2020-05-11T14:49:04Z",
     "ID":"1002"}
 }}],
"phase":"sleeping"}]}
我需要使用jq来过滤列表中状态(.status[].state)为以下格式的休眠项的名称(.metadata.name) 姓名ID 拉吉 到目前为止,我成功地选择了睡眠状态。 jq'.a[].status[]| select(.state |已(“终止”)) 但是,我无法组合名称并生成输出。 如果听起来很傻,请道歉,并提前表示感谢。

使用调用:

jq -r -f sleeping.jq pattern.json
其中sleeping.jq是:

.a[]
| .metadata.name as $raj
| select( any(.status[].state; has("sleeping")) )
| "Name ID \($raj)"
结果是:

Name ID raj
Name ID tom
修正问题的解决办法
欢迎来到SO。到目前为止,您尝试过什么吗?很抱歉,输出出现了一些格式问题。它必须如下名称ID Raj 1002所示
.a[]
| .metadata.name as $raj
| .status[].state
| select( has("sleeping"))
| "Name ID \($raj) \(.sleeping.ID)"