Search code examples
jsonpowershellpowershell-4.0

Extract value from JSON


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 ?


Solution

  • 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