我是json的新手,想从特定的json中提取数据(块(。我试图找到有关如何使用 jq 执行此操作的信息,但到目前为止,我似乎无法得到我想要的。
我的 json:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"squawk" : "6220",
"altitude" : 675,
"seen" : 1.1,
"messages" : 7220,
"tisb" : [],
"hex" : "484a95",
"mlat" : [],
"rssi" : -22
},
{
"hex" : "484846",
"tisb" : [],
"messages" : 20,
"speed" : 89,
"seen" : 0.4,
"squawk" : "7000",
"altitude" : 500,
"rssi" : -23.7,
"track" : 185,
"mlat" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
我想重新格式化此 json,以仅包含包含特定十六进制值的"块"。
例如,我希望我的输出包含 44b5b4 和 00901a:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
有人可以告诉我如何删除所有没有这两个十六进制标识符但仍保持相同 json 结构的项目吗?
多谢!
对数组aircraft
执行select()
,仅匹配所需的十六进制值。map()
函数接受整个数组的输入,选择操作的结果,即基于.hex
值过滤对象|=
更新回原始数组,其余字段保持不变。
jq '.aircraft |= map(select(.hex == "44b5b4" or .hex == "00901a"))' json
选择其hex
与其中一个特定值匹配的块,并更新aircraft
以仅保留这些块。
.aircraft |= map(select(.hex | IN("44b5b4", "00901a")))
在线演示