0

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
  • 1
    Have you tried $jsondeconverted.Param2? Commented Jan 8, 2019 at 11:30

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this and $jsondeconverted.Param2. These methods returned nothing.
@ВасяПупкин Then your problem is somewhere else I would think. Can you try the verifiable complete example I added ?
@Вася Пупкин: Your json is an array. You need to index it like @Sage Pourpre does. $jsondeconverted.Param2will not work.
@PalleDue $jsondeconverted.param2 also work and is equivalent to : $jsondeconverted | Select -ExpandProperty param2. In the simple object above, results would be identical using array index or not. Not using index could result in more than one values in a more complex object though.
@SagePourpre pls, see my edit. Verifable example works, but I can't make it work through code populated variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.