[SOLVED] How can I extract only the name field from json by using gcloud command?

Issue

I am new to gcp and linux. I am trying to get the name from the json format using the gcloud command.
Below is the command and output:

gcloud firestore indexes composite list --format=json

Output:

[
  {
    "fields": [
      {
        "fieldPath": "is_active",
        "order": "ASCENDING"
      },
      {
        "fieldPath": "created_at",
        "order": "DESCENDING"
      },
      {
        "fieldPath": "__name__",
        "order": "ASCENDING"
      }
    ],
    "name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
    "queryScope": "COLLECTION",
    "state": "READY"
  }

How can I get the name value using gcloud command.
I tried using: gcloud firestore indexes composite list | grep name | awk -F'name: ' '{print $2}' but this didn’t give any output.

Please help!

Thanks
Pritish

Solution

Finally, I was able to get the name value using the below command:

gcloud firestore indexes composite list --format=json | grep '"name"' | awk -F' ' '{print $2}' | awk -F'"' '{print $2}'

Output:

projects/emc-ema-simcs-d-xxxxxx/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK

Answered By – Pritish

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.