从JSON中提取值

14

我想使用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,并将其分配给变量。

有人知道如何做吗?

2个回答

19

使用Get-Content命令读取文件,使用ConvertFrom-Json命令进行转换,并直接访问您想要的属性:

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

此参数键现在不是必需的。请查看 https://dev59.com/bFgQ5IYBdhLWcg3wym4Y#54634246。 - Shumi Gupta

3

我不知道为什么,但是上面的消息方法对我不起作用。但是只需在“ConvertFrom-Json”命令后按键获取所需的键即可,例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接