Linux

如何獲取另一個欄位為真的 JSON 對象的特定欄位

  • August 23, 2022

我想從以下命令中獲取特定資訊

[root@test opc]# /bin/oci compute instance list-vnics --instance-id ocid1.instance.oc1.ap-mumbai-1.anrg6ljrkfvuezyc6w4ytv75l55nscz7gg7rwzg55twubrza2uz4j7kzu66a

輸出如下

{
 "data": [
   {
     "availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
     "compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
     "defined-tags": {
       "Oracle-Tags": {
         "CreatedBy": "oracleidentitycloudservice/abcd7@gmail.com",
         "CreatedOn": "2022-08-23T12:08:39.326Z"
       }
     },
     "display-name": "xx",
     "freeform-tags": {},
     "hostname-label": null,
     "id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrdvonadtpflqh2zqd5f4i6nuhppq2pzdvuvr75w4mz6mvy7hdhuia",
     "is-primary": false,
     "lifecycle-state": "AVAILABLE",
     "mac-address": "02:00:17:02:F1:53",
     "nsg-ids": [],
     "private-ip": "100.10.10.227",
     "public-ip": null,
     "skip-source-dest-check": false,
     "subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
     "time-created": "2022-08-23T12:08:41.807000+00:00",
     "vlan-id": null
   },
   {
     "availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
     "compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
     "defined-tags": {
       "Oracle-Tags": {
         "CreatedBy": "oracleidentitycloudservice/abcd@gmail.com",
         "CreatedOn": "2022-08-21T16:40:37.685Z"
       }
     },
     "display-name": "test",
     "freeform-tags": {},
     "hostname-label": "test",
     "id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza",
     "is-primary": true,
     "lifecycle-state": "AVAILABLE",
     "mac-address": "02:00:17:01:DE:74",
     "nsg-ids": [
       "ocid1.networksecuritygroup.oc1.ap-mumbai-1.aaaaaaaadfnpmigo5wnx6ybvjwqziabtzqtgabhsnkbsnv7qjp3euk5jpwva"
     ],
     "private-ip": "100.10.10.214",
     "public-ip": "140.238.162.238",
     "skip-source-dest-check": false,
     "subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
     "time-created": "2022-08-21T16:40:41.532000+00:00",
     "vlan-id": null
   },
   {
     "availability-domain": "ucJX:AP-MUMBAI-1-AD-1",
     "compartment-id": "ocid1.tenancy.oc1..aaaaaaaaqtzldjhvlvbaoheda67fuwkaldini7h6txiglp7yrx2nc4pm3nka",
     "defined-tags": {
       "Oracle-Tags": {
         "CreatedBy": "oracleidentitycloudservice/abcd@gmail.com",
         "CreatedOn": "2022-08-23T12:19:59.985Z"
       }
     },
     "display-name": "yy",
     "freeform-tags": {},
     "hostname-label": null,
     "id": "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljrllmihlbt4rlhsag5jr6hewpofg6fzodmkbqnsdjqdmccyvcetpaa",
     "is-primary": false,
     "lifecycle-state": "AVAILABLE",
     "mac-address": "02:00:17:01:B1:78",
     "nsg-ids": [],
     "private-ip": "100.10.10.43",
     "public-ip": null,
     "skip-source-dest-check": false,
     "subnet-id": "ocid1.subnet.oc1.ap-mumbai-1.aaaaaaaaa2s7zfva3efszqdlacsel7pg2doxcc5yhqt4xt25jrzah5e6ntbq",
     "time-created": "2022-08-23T12:20:03.295000+00:00",
     "vlan-id": null
   }
 ]
}

我希望該部分中的特定 ID"is-primary": true, 值為

id
private-ip

在上述情況下,輸出應為

100.10.10.214
ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza

在這種情況下,有人可以幫助我嗎?謝謝 :)

jq是可用於從 JSON 對像中提取數據的多種工具之一。

  • json¹輸出:
$ jq '.data[]|select(."is-primary")|[.id,."private-ip"]' file.json
[
 "ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza",
 "100.10.10.214"
]
  • .csv 輸出:
$ jq -r '.data[]|select(."is-primary")|[.id,."private-ip"]|@csv' file.json
"ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza","100.10.10.214"
  • 在單獨的行上:
$ jq -r '.data[]|select(."is-primary")|.id + "\n" + ."private-ip"' file.json
ocid1.vnic.oc1.ap-mumbai-1.abrg6ljriaxzes5wtacij7lwhcihv72sva3s5od5ngo6i4v6kv4d6zamypza
100.10.10.214

¹ 從技術上講,如果有多個對象為is-primary真,您將獲得每個對像一個的 json 數組的串聯,您可以將整個jq表達式包裝起來[...]以獲得一個由數組數組組成的 json 輸出。

引用自:https://unix.stackexchange.com/questions/714571