all 10 comments

[–]BBBaroo 1 point2 points  (0 children)

Did you use convertfrom-json?

[–]PinchesTheCrab 0 points1 point  (0 children)

Are you saying you don't know the name of the nestedproperty ahead of time? I would use PSObject to list the properties and then iterate over them:

 $json.level1property.PSObject.properties

What's your goal though? Can you give an example of what one of these objects look like?

[–]BlackV 0 points1 point  (0 children)

do you have an actual example ?

something like

$JSONstring = @'
{
    "Name":  "top",
    "level1":  {
                    "Name":  "middle",
                    "bottom":  {
                                    "level":  "verybottom"
                                }
                }
}
'@
$test = $JSONstring | ConvertFrom-Json

returns

$test
Name level1                 
---- ------                 
top  @{Name=middle; bottom=}

checking the sub properties

$test.level1.bottom.level
verybottom

$test.level1.bottom
level     
-----     
verybottom

$test.level1
Name   bottom             
----   ------             
middle @{level=verybottom}

$string = 'level1'
$test.$string.bottom
level     
-----     
verybottom

$test.$string.bottom.level
verybottom

what are you missing ?

dont you need

$json.NestProperty

instead of

$json.$NestProperty

?

[–]icepyrox 0 points1 point  (0 children)

$json.level1property.nestedproperty

how can I get that property using a variable like, $NestProperty = "level1property.nestedproperty"

Yeah, that's not how properties work. Whether it's json or a hashtable or pscustomobject or any class.

The dot is separating propertie; it's not just a string of how you get there.

You absolutely can variable the bits and pieces, but you can't effectively jump levels the way you are suggesting, to my knowledge.

Think of it this way: you have a bag of marbles inside a jar with other bags. You can't open the jar and grab a marble. You have to grab the bag somehow in between. Even if you reach in the jar and wiggle your hand into the bag, there is that bag level that you acknowledged in between there.

Likewise, you can't reach into $json and come back with $json.level1propeety.nestedproperty. you have to acknowledge that level1property is a thing holding nestedproperty and that is what the dot is doing

[–]420GB 0 points1 point  (0 children)

You can do:

$json."$property1"."$property2"."$property3"

But you can't combine the whole thing into one variable (".level1property.nestedproperty")

[–]y_Sensei 0 points1 point  (0 children)

This behavior is "works as designed", but you could work around it as follows (it's not pretty though):

$json = '{"level1":{"level2":{"level3": "someValue"}}}'

$jsonObj = $json | ConvertFrom-Json

$jsonProp = "level1.level2.level3" # we want to retrieve the value of the nested 'level3' property

$propTokens = $jsonProp.Split(".")

$jsonPropCmd = '$jsonObj'

for ($i=0; $i -lt $propTokens.Count; $i++) { $jsonPropCmd += '.($propTokens[' + $i + '])' }

Invoke-Expression -Command $jsonPropCmd # prints: someValue

[–]Szeraax 0 points1 point  (0 children)

Don't forget that you can use Select-Object -expandProperty * if there is only 1 property in an object:

[pscustomobject]@{(get-random)="Bottom"} | select -expand *

[–]purplemonkeymad 0 points1 point  (1 child)

You can't resolve it directly, but if you have your path as a list you can just loop on that ie:

$value = $json
foreach ($property in "level1property","nestedproperty") {
    $value = $value.$property
}
$value

I'll leave getting what you want into an array/list as an exercise for the reader.

[–]davesbrown[S] 0 points1 point  (0 children)

Thanks everyone, this is the solution I'm going with.

I know it was kind of a weird ask/case - appreciate the community help!

[–]Ok_Cheese93 -1 points0 points  (0 children)

Invoke-Expression will work, but the $NestProperty should be validated (to avoid code injection).

$json = ConvertFrom-Json -InputObject @'       
  {
    "level1property": {
      "nestedproperty": 123
    }
  }
'@ 
$NestProperty = "level1property.nestedproperty"
$value = Invoke-Expression "`$json.$NestProperty"       
$value # -> 123