【问题标题】:Extract value from JSON从 JSON 中提取值
【发布时间】:2023-04-04 08:29:01
【问题描述】:

我正在尝试使用 PowerShell 从 JSON 对象中提取值,我有以下 JSON:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
     "clusterName": {
      "value": "hbasehd35"
    },
     "location": {
      "value": "East US 2"
    },
     "clusterType": {
      "value": "hbase"
    },
     "clusterVersion": {
      "value": "3.5"
    },
     "clusterWorkerNodeCount": {
      "value": 5
    },
     "subnetName": {
      "value": "hbase-subnet"
    },
     "headNodeSize": {
      "value": "Standard_D12_v2"
    },
     "workerNodeSize": {
      "value": "Standard_D12_v2"
    },
     "zookeeperSize": {
      "value": "Large"
    },
     "clusterStorageAccountName": {
      "value": "hbasestorage"
    },
     "storageAccountType": {
      "value": "Standard_GRS"
    },
     "Environment": {
      "value": "test"
    }
  }
}

这里我想使用powershell从这个文件中提取clusterStorageAccountName,并将其分配给变量。

有人知道怎么做吗?

【问题讨论】:

    标签: json powershell powershell-4.0


    【解决方案1】:

    使用Get-Content cmdlet 读取文件,使用ConvertFrom-Json cmdlet 对其进行转换,然后访问所需的属性:

    $yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).parameters.clusterStorageAccountName.value
    

    【讨论】:

    【解决方案2】:

    我不知道为什么,但是上面消息中的方法对我不起作用。但在“ConvertFrom-Json”命令之后,只需通过密钥获取您需要的密钥,例如:

    $yourVariable = (Get-Content 'yourJsonFilePath.json' | ConvertFrom-Json).clusterStorageAccountName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2015-12-19
      相关资源
      最近更新 更多