使用 jq 提取文本

使用 jq 提取文本

如何从以下 JSON 中jq提取包含 11 的对象id,以处理 CPU 封装温度?

{
"id":0,
"Text":"Sensor",
"Children":[
{
"id":1,
"Text":"PC15029",
"Children":[
{
"id":2,
"Text":"HP 1993",
"Children":[
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/mainboard.png"
},
{
"id":3,
"Text":"Intel Core i5-4200M",
"Children":[
{
"id":4,
"Text":"Clocks",
"Children":[
{
"id":5,
"Text":"Bus Speed",
"Children":[
],
"Min":"100 MHz",
"Value":"100 MHz",
"Max":"100 MHz",
"ImageURL":"images/transparent.png"
},
{
"id":6,
"Text":"CPU Core #1",
"Children":[
],
"Min":"2694 MHz",
"Value":"2993 MHz",
"Max":"3093 MHz",
"ImageURL":"images/transparent.png"
},
{
"id":7,
"Text":"CPU Core #2",
"Children":[
],
"Min":"2694 MHz",
"Value":"2993 MHz",
"Max":"3093 MHz",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/clock.png"
},
{
"id":8,
"Text":"Temperatures",
"Children":[
{
"id":9,
"Text":"CPU Core #1",
"Children":[
],
"Min":"39,0 °C",
"Value":"56,0 °C",
"Max":"65,0 °C",
"ImageURL":"images/transparent.png"
},
{
"id":10,
"Text":"CPU Core #2",
"Children":[
],
"Min":"42,0 °C",
"Value":"58,0 °C",
"Max":"64,0 °C",
"ImageURL":"images/transparent.png"
},
{
"id":11,
"Text":"CPU Package",
"Children":[
],
"Min":"43,0 °C",
"Value":"58,0 °C",
"Max":"65,0 °C",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/temperature.png"
},
{
"id":12,
"Text":"Load",
"Children":[
{
"id":13,
"Text":"CPU Total",
"Children":[
],
"Min":"0,0 %",
"Value":"46,9 %",
"Max":"100,0 %",
"ImageURL":"images/transparent.png"
},
{
"id":14,
"Text":"CPU Core #1",
"Children":[
],
"Min":"0,0 %",
"Value":"43,8 %",
"Max":"100,0 %",
"ImageURL":"images/transparent.png"
},
{
"id":15,
"Text":"CPU Core #2",
"Children":[
],
"Min":"0,0 %",
"Value":"50,0 %",
"Max":"100,0 %",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/load.png"
},
{
"id":16,
"Text":"Powers",
"Children":[
{
"id":17,
"Text":"CPU Package",
"Children":[
],
"Min":"5,0 W",
"Value":"18,8 W",
"Max":"23,8 W",
"ImageURL":"images/transparent.png"
},
{
"id":18,
"Text":"CPU Cores",
"Children":[
],
"Min":"0,6 W",
"Value":"9,3 W",
"Max":"14,0 W",
"ImageURL":"images/transparent.png"
},
{
"id":19,
"Text":"CPU Graphics",
"Children":[
],
"Min":"0,0 W",
"Value":"1,9 W",
"Max":"6,2 W",
"ImageURL":"images/transparent.png"
},
{
"id":20,
"Text":"CPU DRAM",
"Children":[
],
"Min":"0,8 W",
"Value":"1,6 W",
"Max":"2,3 W",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/power.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/cpu.png"
},
{
"id":21,
"Text":"Generic Memory",
"Children":[
{
"id":22,
"Text":"Load",
"Children":[
{
"id":23,
"Text":"Memory",
"Children":[
],
"Min":"42,4 %",
"Value":"48,3 %",
"Max":"52,1 %",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/load.png"
},
{
"id":24,
"Text":"Data",
"Children":[
{
"id":25,
"Text":"Used Memory",
"Children":[
],
"Min":"3,4 GB",
"Value":"3,8 GB",
"Max":"4,1 GB",
"ImageURL":"images/transparent.png"
},
{
"id":26,
"Text":"Available Memory",
"Children":[
],
"Min":"3,8 GB",
"Value":"4,1 GB",
"Max":"4,6 GB",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/power.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/ram.png"
},
{
"id":27,
"Text":"ST500LM021-1KJ152",
"Children":[
{
"id":28,
"Text":"Temperatures",
"Children":[
{
"id":29,
"Text":"Temperature",
"Children":[
],
"Min":"37,0 °C",
"Value":"38,0 °C",
"Max":"41,0 °C",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/temperature.png"
},
{
"id":30,
"Text":"Load",
"Children":[
{
"id":31,
"Text":"Used Space",
"Children":[
],
"Min":"77,7 %",
"Value":"77,7 %",
"Max":"78,2 %",
"ImageURL":"images/transparent.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/load.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/hdd.png"
}
],
"Min":"",
"Value":"",
"Max":"",
"ImageURL":"images_icon/computer.png"
}
],
"Min":"Min",
"Value":"Value",
"Max":"Max",
"ImageURL":""
}

答案1

假设您想提取id等于 的对象11,其中您的数据位于xxx.js

$ jq 'recurse(.Children[]) | select(.id == 11)' < xxx.js
{
  "id": 11,
  "Text": "CPU Package",
  "Children": [],
  "Min": "43,0 °C",
  "Value": "58,0 °C",
  "Max": "65,0 °C",
  "ImageURL": "images/transparent.png"
}

答案2

如果不知道它是您想要的 ID 11 的条目,则您需要获取带有文本字段的所有条目Temperatures,然后选择Children其中带有文本字段的条目CPU Package

$ jq 'recurse(.Children[]) | select(.Text == "Temperatures").Children[] | select(.Text == "CPU Package")' file
{
  "id": 11,
  "Text": "CPU Package",
  "Children": [],
  "Min": "43,0 °C",
  "Value": "58,0 °C",
  "Max": "65,0 °C",
  "ImageURL": "images/transparent.png"
}

相关内容