簡體   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