通过jq从JSON格式数据中获取部分数据

通过jq从JSON格式数据中获取部分数据

我有一个 JSON 格式数据示例

[
{
    "info":{
        "disk_num":"2",
        "hostname":"hostA",
        "net":[
            "eth0",
            "eth1"
        ],
        "software":{
            "version":"1.0.0",
            "build_date":"2021-10-08",
            "commit":"bfdaee"
        },
        "kernel":"5.0.0"
    },
    "ip":"10.240.82.192",
    "status":"PASS"
},
{
    "info":{
        "disk_num":"2",
        "hostname":"hostA",
        "net":[
            "eth0",
            "eth1"
        ],
        "software":{
            "version":"1.0.0",
            "build_date":"2021-10-08",
            "commit":"bfdaee"
        },
        "kernel":"5.0.3"
    },
    "ip":"10.240.82.193",
    "status":"PASS"
},
{
    "info":{
        "disk_num":"5",
        "hostname":"hostB",
        "net":[
            "eth0",
            "eth1"
        ],
        "software":{
            "version":"1.0.2",
            "build_date":"2021-10-11",
            "commit":"935678"
        },
        "kernel":"5.0.1"
    },
    "ip":"10.240.82.194",
    "status":"PASS"
}
]

我想获取部分数据并构造另一个 json FORMAT Like

[
    {
      "IP": "10.240.82.192",
      "STATUS": "PASS",
      "SOFTWARE": "1.0.0",
      "KERNEL": "5.0.0"
    },
    {
      "IP": "10.240.82.193",
      "STATUS": "PASS",
      "SOFTWARE": "1.0.0",
      "KERNEL": "5.0.3"
    },
    {
      "IP": "10.240.82.194",
      "STATUS": "PASS",
      "SOFTWARE": "1.0.2",
      "KERNEL": "5.0.1"
    }
]

我已经尝试过这个命令(我已将示例数据保存到 json.data)

cat json.data|jq -r '{IP:.[].ip,STATUS:.[].status,SOFTWARE:.[].info.software.version,KERNEL:.[].info.kernel}'

它不起作用,它将输出如下:

{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.192",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.193",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.0",
  "KERNEL": "5.0.1"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.0"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.3"
}
{
  "IP": "10.240.82.194",
  "STATUS": "PASS",
  "SOFTWARE": "1.0.2",
  "KERNEL": "5.0.1"
}

我想知道正确的 cli 命令来获得我想要的

非常感谢

答案1

对于您指定的四个元素中的每一个,您的命令都会迭代每个数组元素,因此您将获得 3 4 = 81 个对象,而不是三个对象。

要迭代每个对象,您可以使用一个map()函数,如下所示:

jq 'map({IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel})' json.data

[
  {
    "IP": "10.240.82.192",
    "STATUS": "PASS",
    "SOFTWARE": "1.0.0",
    "KERNEL": "5.0.0"
  },
  {
    "IP": "10.240.82.193",
    "STATUS": "PASS",
    "SOFTWARE": "1.0.0",
    "KERNEL": "5.0.3"
  },
  {
    "IP": "10.240.82.194",
    "STATUS": "PASS",
    "SOFTWARE": "1.0.2",
    "KERNEL": "5.0.1"
  }
]

答案2

@roaima 的回答很好地解释了这一点,或者你也可以这样编写解决方案:

jq '[ .[] | {IP:.ip, STATUS:.status, SOFTWARE:.info.software.version, KERNEL:.info.kernel} ]' json.data

在这里,该.[]部分迭代输入的所有元素,而[]包装整个结果的外部部分将其转换为数组。

相关内容