简体   繁体   中英

Problem in getting json key value in nested json using jq

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

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

I solved issue with below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM