How to Filter and Format OCI CLI Data

All commands of OCI CLI return JSON data. It’s nice but it’s kind of hard to read. Let’s see how we can format it just for readability.

We are going to choose oci compute image list command in this example.

$ oci compute image list --compartment-id $T

Make sure you have $T set to your tenancy OCID. As a result of the command, you will see data like the following.

"data": [
"agent-features": null,
"base-image-id": null,
"compartment-id": null,
"create-image-allowed": true,
"defined-tags": {},
"display-name": "Windows-Server-2016-Standard-Edition-VM-Gen2-2020.01.14-0",
"freeform-tags": {},
"id": "ocid1.image.oc1.iad.aaaaaaaauwpipy7yex62fvqix7a7ipdzdhc6pdz57vkowvc4jhkfrazm6bwa",
"launch-mode": "NATIVE",
"launch-options": {
"boot-volume-type": "PARAVIRTUALIZED",
"firmware": "UEFI_64",
"is-consistent-volume-naming-enabled": false,
"is-pv-encryption-in-transit-enabled": true,
"network-type": "VFIO",
"remote-data-volume-type": "PARAVIRTUALIZED"
"lifecycle-state": "AVAILABLE",
"operating-system": "Windows",
"operating-system-version": "Server 2016 Standard",
"size-in-mbs": 262144,
"time-created": "2020-01-15T00:08:30.361000+00:00"

I’m going to just extract display-name and operationg-system as a table formatted data.

oci compute image list --compartment-id $T --query "data[*]".{"name:\"display-name\",os:\"operating-system\""} --output table

Here is the output.

Let’s do a basic filtering of the data. We will list only the images where the operating-system is Oracle Linux.

oci compute image list --compartment-id $T --query "data[?\"operating-system\"=='Oracle Linux']".{"name:\"display-name\",os:\"operating-system\""} --output table

The filtering looks kind of cryptic because of the dash in the JSON data key. Output looks like the image below.

It’s quite useful to be able to filter the data and also format it in a better looking table format.

I hope this helps someone out there because I struggled with the –query part of this effort because of the dashed JSON data key.

Author: admin

A software engineer in greater Seattle area

Leave a Reply

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