繁体   English   中英

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

[英]Problem in getting json key value in nested json using jq

目标:尝试使用jq function 从 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"
    }
}
]

我尝试了什么:

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

如何从上述 json 中获取键name值。 我需要搜索的键没有更深的嵌套子部分。 请帮助找到解决此问题的方法

如果您不想为必须找出感兴趣的密钥的路径而烦恼,并且如果您不介意特定密钥名称可能多次出现的可能性,那么以下单行可能值得考虑:

.. | objects | select(.name).name

我解决了以下问题

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

暂无
暂无

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

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