Get value from json variable using windows powershell 5.1

653 Views Asked by At

I've got a JSON like this

[  
   {  
      "Param1":true,
      "Param2":0,
      "Param3":"OK"
      ...
      ...
   }
]

How can I get Param2 value, using powershell 5.1? For now, I tried to get property names, but only get length

$jsondeconverted = $jsonOrig | ConvertFrom-Json
$jsonOrig .PsObject.Properties |
     Select-Object -ExpandProperty Name |
     ForEach-Object {
        Write-Host "Key : " $_
        Write-Host "Value : " $thisJSON."$_"
     }

EDIT This is how I get my json

$jsonvar = '['+$jsonvar+']'
$convertedJson =  $jsonvar | ConvertTo-Json -Depth 10
$deconvertedJson = $convertedJson | ConvertFrom-Json

$deconvertedJson contains only length parameter and nothing more.

1

There are 1 best solutions below

6
On

You need to look into the object ($jsondeconverted) rather than the string ($jsonOrig)

Based on your json Structure, you would access param2 in the following way $jsondeconverted[0].Param2

Verifiable complete example

$jsonorig = '[{"Param1":true,"Param2":0,"Param3":"OK"}]'
$jsondeconverted = $jsonorig | ConvertFrom-Json
$jsondeconverted[0].param2