You can find Key1A
or whatever key you are looking for by looking at the definition column of Get-Member
.
Let's define your JSON As variable $TestJson
:
$testJson = @"
{
"key1":{
"key1A":{
"someKey":"someValue"
},
"key1B":{
"someKey":"someValue"
}
},
"key2":{
"key2A":{
"someKey":"someValue"
},
"key2B":{
"someKey":"someValue"
}
},
"key3":{
"key3A":{
"someKey":"someValue"
},
"key3B":{
"someKey":"someValue"
}
}
}
"@
$testJson = $testJson | ConvertFrom-Json
We are looking for Key1A
in $testJson
which we do not know is under the parent node key1
, we can do this by looking at the output of $testJson | gm
$testJson | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
key1 NoteProperty System.Management.Automation.PSCustomObject key1=@{key1A=; key1B=}
key2 NoteProperty System.Management.Automation.PSCustomObject key2=@{key2A=; key2B=}
key3 NoteProperty System.Management.Automation.PSCustomObject key3=@{key3A=; key3B=}
We can see here that all of the nodes and their sub-nodes are listed in the definitions tab, with bigger JSONs, we wouldn't be able to see the whole definitions tab with this so we could wither to one of these two things:
$testJson | gm | select-object "Definition"
($testJson | gm).Definition
So if we want to find Key1A
we can do
$testJson | gm | ? {$_.Definition -imatch "key1A"}
Which finds the definition in where key1a
is in (case-insensitive as specified by -i
instead of -c
) which gives us the output of
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
key1 NoteProperty System.Management.Automation.PSCustomObject key1=@{key1A=; key1B=}
Where as you can see the parent node is key1
and we can grab that as well with
($testJson | gm | ? {$_.Definition -imatch "key1A"}).name
key1
And to view the content of key1
we can do
$($testJson).$(($testJson | gm | ? {$_.Definition -imatch "key1A"}).name)
key1A key1B
----- -----
@{someKey=someValue} @{someKey=someValue}
And key1a
$($testJson).$(($testJson | gm | ? {$_.Definition -imatch "key1A"}).name).key1a
someKey
-------
someValue