Saving You 15 Minutes - Formatting JSON output from Oracle Cloud CLI with Powershell Part 1
Saving You 15 Minutes - Formatting JSON output from Oracle Cloud CLI with Powershell Part 1
By Philip Brown
On 31 May 2018

When you’re running an Oracle Cloud account and you have more than one VCN, one VM or one subnet then using the console to view / manage your estate becomes slightly unwieldy. It’s not just Oracle Cloud that suffers from this but other Clouds as well.

The Oracle Cloud CLI provides you a command line interface to run commands to query, create and view Cloud resources. It’s a handy but the output is JSON or table format with no way (that I have seen at least) to reduce the output into exactly what is needed.

If you’re running the CLI from the command line you can use Powershell to format the output. It’s pretty simple once you know how. This is my first foray into Powershell so this might not be ‘best practice’ but at least enough to get you going. For me I used Powershell as it came as part of my laptop base install and doesn’t require me to install or configure any other client tools.

I’m not going to cover the deployment of the CLI tool itself as it is already well documented here

To view a list of instances in JSON run the following command

oci compute instance list --compartment-id ocid1.compartment.oc1..XXXXX
The JSON output will be like this but LOTS of it…
"display-name": "ldndb01",
      "extended-metadata": {},
      "freeform-tags": {},
      "id": "ocid1.instance.oc1.eu-frankfurt-1.ab

If you set the output of the CLI to a variable ($OUTPUT) in Powershell you can start to manipulate it.

$output=oci compute instance list --compartment-id ocid1.compartment.oc1..XXXX

Now you can echo $OUTPUT and see the info. Powershell has a command called ConvertFrom-Json and here you can pipe the previous command into that to convert the format.

$output=oci compute instance list --compartment-id ocid1.compartment.oc1..XXXX | ConvertFrom-Json

Now when you echo the $OUTPUT you will see something like this

PS C:\Users\phili> echo $output
Data
----
{@{availability-domain=IrvI:EU-FRANKFURT-1-AD-1; compartment-id=o

This is the bit which will save you some time; the output is saved an array and to be able to manipulate that array you can use the following command

PS C:\Users\phili> echo $output.data
availability-domain : IrvI:EU-FRANKFURT-1-AD-1
compartment-id      : ocid1.compartment.oc1..XXXX
defined-tags        :
display-name        : ldndb01

The full output will be the same but now no longer in JSON format. Now the great thing you can do from this is select the columns from the array using SQL like syntax

PS C:\Users\phili> echo $output.data | select 'display-name', 'lifecycle-state'

display-name              lifecycle-state
------------              ---------------
ldndb01		           	RUNNING
ldndb02        		RUNNING

So from here you can now use the CLI to select the exact data you require; all you need to do now is go through the CLI documentation to get the relevant commands.

Hopefully it will save you a moment or two. In the next part I will show you have to resolve issues with nested JSON which some of the CLI will return; i.e. security lists and ingress and egress rules.

Read Part 1

Get in touch

We always put our customers first. Contact us by using the form below, and we will get back to you as soon as we can.

Please make sure to use a valid company email.
Please make sure to use a valid email format.

Data Intensity Solutions you are interested in (Select one that applies):

Submit
Please make sure all the mandatory fields marked red are filled.

Clicking this button tells us that you also agree to our privacy policy and cookie policy. You can opt out of these communications at any time.