Home > Enterprise >  Problem in getting json key value in nested json using jq
Problem in getting json key value in nested json using jq

Time:07-30

Objective: Trying to get a key's value from a json file using jq function. Input source is a file and need to fetch specific key value to use it in further process of the flow

Input Json:

[
  {
    "accessTier": "Hot",
    "allowBlobPublicAccess": false,
    "allowCrossTenantReplication": null,
    "enableNfsV3": false,
    "encryption": {
      "encryptionIdentity": null,
      "keySource": "Microsoft.Storage",
      "keyVaultProperties": null,
      "requireInfrastructureEncryption": null,
      "services": {
        "blob": {
          "enabled": true,
          "keyType": "Account",
          "lastEnabledTime": "xxxxxx"
        },
        "file": {
          "enabled": true,
          "keyType": "Account",
          "lastEnabledTime": "xxxxxx"
        },
        "queue": null,
        "table": null
      }
    },
    "extendedLocation": null,
    "failoverInProgress": null,
    "geoReplicationStats": null,
    "id": "/subscriptions/xxxx-xxxx-xxxxx-xxxx/resourceGroups/xxxxxxxxxxxxe/providers/Microsoft.Storage/storageAccounts/xxxxxxxxxxxx",
    "identity": {
      "principalId": null,
      "tenantId": null,
      "type": "None",
      "userAssignedIdentities": null
    },
    "immutableStorageWithVersioning": null,
    "isHnsEnabled": true,
    "isLocalUserEnabled": null,
    "isSftpEnabled": null,
    "keyCreationTime": {
      "key1": "xxxxxxx",
      "key2": "xxxxxxx"
    },
    "keyPolicy": null,
    "kind": "StorageV2",
    "largeFileSharesState": null,
    "lastGeoFailoverTime": null,
    "location": "xxxxxxxx",
    "minimumTlsVersion": "TLS1_0",
    "name": "storageaccountfortest",
    "networkRuleSet": {
      "bypass": "xxxxxxx",
      "defaultAction": "Allow",
      "ipRules": [],
      "resourceAccessRules": null,
      "virtualNetworkRules": []
    },
    "primaryLocation": "xxxxxxxxx",
    "privateEndpointConnections": [],
    "provisioningState": "Succeeded",
    "publicNetworkAccess": null,
    "resourceGroup": "xxxxxxxxx",
    "routingPreference": null,
    "sasPolicy": null,
    "secondaryEndpoints": null,
    "secondaryLocation": null,
    "sku": {
      "name": "Standard_LRS",
      "tier": "Standard"
    }
}
]

What I tried:

user@ablab:~$ jq '.name' input.json
jq: error (at input.json:100): Cannot index array with string "name"
user@ablab:~$

How to get key name value from above mentioned json. There is no deeper nested subsection of keys where I need to search. Please help to find how to fix this issue

CodePudding user response:

I solved issue with below

user@ablab:~$ jq -r '.[] | .name' input.json

CodePudding user response:

If you don't want to be bothered with having to figure out the path to the key of interest, and if you don't mind the possibility that there might be several occurrences of a particular key name, then the following one-liner may be worth considering:

.. | objects | select(.name).name
  • Related