Parse json response from curl in github action

Viewed 750

I have the curl request as shown below and also attached the response of curl request from the response how can I fetch the value which is under "status.azure.resource_name" and store it in some variable as I'm new to GitHub action facing some challenges if this is some programming api I could have resolved it

Request:

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

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": []
    }
  }
}
1 Answers

There are multiple ways to achieve that.

  1. You can use directly jq and read variable in bash step.

  2. You can use one of existing open source actions from marketplace:

- name: get nested property
    id: format_script
    uses: notiz-dev/github-action-json-property@release
    with: 
        path: 'yourjson.json'
        prop_path: 'status.azure.resource_name'
- run: echo ${{steps.format_script.outputs.prop}} 
  1. Download just one property from whole JSON if you don't need a file itself:
- uses: senmu/download-json-property-action@v1.0.0
  with:
    url: 'https://httpbin.org/json'
    property_path: status.azure.resource_name
Related