Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 296
roy Avatar asked Feb 14 '17 12:02

roy


2 Answers

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
like image 139
Martin Brandl Avatar answered Oct 16 '22 23:10

Martin Brandl


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
like image 44
Ustin Avatar answered Oct 17 '22 00:10

Ustin