How to Flatten OCI CLI Data

OCI CLI is a very convenient way to get your cloud resource data. Quite often, the data that gets returned from the API is nested. For example, when you get the data on your VM, the command and the data that gets returned looks like this.

oci compute instance list -c $tenancy_id --region ap-tokyo-1

* I have my instance right under the root compartment (tenancy).

Here is the data that gets returned by executing the command.

{
  "data": [
    {
      <SNIP>
      "display-name": "tokyo-proxy",
      "extended-metadata": {},
      "fault-domain": "FAULT-DOMAIN-1",
      "freeform-tags": {},    
      <SNIP>
      "lifecycle-state": "STOPPED",
      "metadata": {
        "ssh_authorized_keys": "ssh-rsa fakepublickeyhere  hiriumi@hayato-mac-mini.local\n"
      },
      "region": "ap-tokyo-1",
      "shape": "VM.Standard.E3.Flex",
      "shape-config": {
        "gpu-description": null,
        "gpus": 0,
        "local-disk-description": null,
        "local-disks": 0,
        "local-disks-total-size-in-gbs": null,
        "max-vnic-attachments": 2,
        "memory-in-gbs": 2.0,
        "networking-bandwidth-in-gbps": 1.0,
        "ocpus": 1.0,
        "processor-description": "2.25 GHz AMD EPYC\u2122 7742 (Rome)"
      },
      "source-details": {
        "boot-volume-size-in-gbs": null,
        "image-id": "ocid1.image.oc1.ap-tokyo-1.aaaaaaaavnulbem7sdol6pzlqadzwrcej2yyg3negvbz7ullnsoaspluzydq",
        "kms-key-id": null,
        "source-type": "image"
      },
      "system-tags": {},
      "time-created": "2020-11-14T07:29:26.033000+00:00",
      "time-maintenance-reboot-due": null
    }
  ]
}

Notice that the highlighted lines are the nested data. To limit the data to a few data points, you could do something like this.

oci compute instance list --region ap-tokyo-1 -c $tenancy_id \
--query 'data[]'.{'name:"display-name",shape_config:"shape-config"'} --output table

By adding –query option, you can limit the data and by adding –output table, you can format the data in an easy-to-see format but the output does not look good because the shape-config node has child nodes.

------------------------------------------------------------------------------------------+
| name        | shape_config                                                                                                                                                                                                                                                                                 |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tokyo-proxy | {'ocpus': 1.0, 'memory-in-gbs': 2.0, 'processor-description': '2.25 GHz AMD EPYC™ 7742 (Rome)', 'networking-bandwidth-in-gbps': 1.0, 'max-vnic-attachments': 2, 'gpus': 0, 'gpu-description': None, 'local-disks': 0, 'local-disks-total-size-in-gbs': None, 'local-disk-description': None} |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

By executing the following command, we can flatten the child nodes and limit the data to display even further.

oci compute instance list -c $tenancy_id --region ap-tokyo-1 \
--query 'data[]'.{'name:"display-name",cores:"shape-config".ocpus,memory:"shape-config"."memory-in-gbs"'} --output table

Here is the sample output.

+-------+--------+-------------+
| cores | memory | name        |
+-------+--------+-------------+
| 1.0   | 2.0    | tokyo-proxy |
+-------+--------+-------------+

It took me a while to figure this out so I thought I’d share it here in case someone is looking for a way to flatten the nested data using OCI CLI.

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *