Tirumalesh
Tirumalesh

Reputation: 95

Fetch json field value from curl response in github action

I have the below curl request and also attached the response from the response I need to extract the value for "status.azure.resource_name" I'm new to GitHub action so stuck with this issue from more than 8 hours please help on this if it is in some programming languages I would have resolved by now

Curl request:

curl --location --request PUT $URL \
                                          --header "$AUTH_HEADER" \
                                          --header 'Content-Type: application/json' \
                                          --data-raw "$PAYLOAD)"

And the response:

{
  "labels": {},
  "spec": {
    "mysql": {
      "version": "8.0",
      "sku": {
        "name": "GP_Gen5_4"
      },
      "storage_profile": {
        "storage_mb": 5120
      }
    },
    "key_vault": {
      "access_policies": [
        {
          "name": "test",
          "type": "group",
          "project": null
        }
      ]
    }
  },
  "type": "azure-mysql",
  "name": "mysql",
  "id": "1234",
  "created_at": "2012-03-04T10:00:05+00:00",
  "updated_at": "2012-03-04T10:00:05+00:00",
  "project": {
    "id": "ae3dfa99",
    "name": "Test",
    "url": "www.google.com",
    "geography": "in"
  },
  "links": {
    "key_vault": {
      "endpoint": {
        "url": "",
        "description": "test.",
        "display_name": "test"
      },
      "azure_portal": {
        "url": "test",
        "description": "Link to the resource in the Azure portal.",
        "display_name": "Key Vault Azure Portal"
      }
    },
    "azure_portal": {
      "url": "test",
      "description": "Link to the resource in the Azure portal.",
      "display_name": "Azure Portal"
    },
    "endpoint": {
      "url": "test",
      "description": "Azure resource endpoint.",
      "display_name": "test"
    }
  },
  "url": "test",
  "tags": {
    "cost_center_id": "471000",
    "customer": "internal",
    "product_group": "internal",
    "environment_type": "test",
    "budget_category": "",
    "team": ""
  },
  "spiffe_id": "test",
  "status": {
    "ready": false,
    "state": "reconciling",
    "deployment": {
      "steps": {}
    },
    "azure": {
      "resource_name": "test",
      "id": null,
      "subscription_id": "test",
      "resource_group": "test"
    },
    "key_vault": {
      "access_policies": []
    }
  }
}

Upvotes: 1

Views: 1878

Answers (1)

chenrui
chenrui

Reputation: 9866

You can just use jq to do the json parsing. It is already available in the github action runner.

In your case it would be like this (I pasted your response json into test.json file)

$ cat test.json | jq .status.azure.resource_name
"test"

Upvotes: 1

Related Questions