简体   繁体   English

使用 jq 在嵌套 json 中获取 json 键值的问题

[英]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.目标:尝试使用jq function 从 json 文件中获取密钥的值。 Input source is a file and need to fetch specific key value to use it in further process of the flow输入源是一个文件,需要获取特定的键值以在流程的进一步处理中使用它

Input Json:输入 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.如何从上述 json 中获取键name值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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