Extract value from JSON

被刻印的时光 ゝ 提交于 2020-01-21 05:06:31

问题


I am trying to use PowerShell to extract value from JSON object, I have the following 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"
    }
  }
}

Here I want to extract clusterStorageAccountName from this file using powershell, and assign it to variable.

Anyone know how to do this ?


回答1:


Use the Get-Content cmdlet to read the file, convert it using the ConvertFrom-Json cmdlet and just access the property you want:

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



回答2:


I don't know why, but the approach from message above don't work for me. But works just getting key that you need by key after "ConvertFrom-Json" command, e.g.:

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


来源:https://stackoverflow.com/questions/42225941/extract-value-from-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!