0

I have a sample JSON format data

[
{
    "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"
}
]

I want to get partial data and construct another 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"
    }
]

I have tried this command (I have saved sample data to json.data)

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

It didn't work , it will output as the followings:

{
  "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"
}

I wonder to know the correct cli command to get what I want

Thanks very much

0

2 Answers 2

2

Your command iterates across each of the array elements, for each of the four elements you've specified, so instead of getting three objects, you're getting 34 = 81 objects.

To iterate across each object you can use a map() function, like this:

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"
  }
]
1
  • Great solution ! Thanks very much Oct 19, 2021 at 1:47
0

@roaima's answer explains it well, alternatively you can write the solution like this:

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

Here, the .[] part iterates over all elements of input, and the outer [] part, that wraps the whole result, converts it to an array.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .