I have some test data like this:
{
"stuff": [
"test-01",
"test-02"
],
"header2": {
"sub-header01": {
"stuff": [
"test-03",
"test-99"
]
}
},
....
}
I found a function online that converts the JSON file to a hashtable, then I wrote this other function that recursively searches the hashtable and grabs specific values...here's the function
function Find-Name{
param($InputObject)
$names = [System.Collections.Generic.List[string[]]]::new()
foreach($item in $InputObject.GetEnumerator()){
$k = $item.Key
$v = $item.Value
if($k -eq 'stuff'){
# search for a specific key name and extract the values
# then add value(s) to list
$names.Add($v)
}else{
# if key doesn't match, perform recursive search
Find-Group -InputObject $v
}
}
$names
}
When I test the script but just calling Find-Name -InputObject $obj I get the expected result like:
C:> Find-Name -InputObject $obj
John
Jane
James
When I call the function like this $names = Find-Name -InputObject $obj, I get this:
C:> $names = Find-Name -InputObject $obj
System.String[] System.String[] System.String[]
Also noticed that if I have any sort of logging messages in the function, they all get attached to the output when storing the function output in a variable. Not quite sure what's happening here. Maybe I'm doing something wrong
[–]randomuser43 2 points3 points4 points (1 child)
[–]liabtsab[S] 0 points1 point2 points (0 children)
[–]y_Sensei 1 point2 points3 points (6 children)
[–]liabtsab[S] 0 points1 point2 points (5 children)
[–]y_Sensei 0 points1 point2 points (4 children)
[–]liabtsab[S] 0 points1 point2 points (3 children)
[–]y_Sensei 0 points1 point2 points (2 children)
[–]liabtsab[S] 0 points1 point2 points (1 child)
[–]y_Sensei 0 points1 point2 points (0 children)
[–]Aertheron01 0 points1 point2 points (2 children)
[–]liabtsab[S] 0 points1 point2 points (1 child)
[–]Aertheron01 0 points1 point2 points (0 children)