I am using an api to gather a list of comments for particular tickets, the tickets have nested comments which are returned from the api like so:
{
"comments": [
{
"text": "test 1",
"comments": [
{
"text": "test 2",
"comments": [
{
"text": "test 3",
"comments":[]
}
]
}
]
}
]
}
The number of child comments can be n for any parent comment. I'm ultimately trying to get the text value for each "comments" tag until the "comments" tag is null.
I thought about making a parent object then trying to append the property search until it returns null.
$n = 1
$exists = $true
while ($exists){
$string = ".comments"
$search = $string * $n
$search = $search.Substring(1)
$m = $i.$search
$commentVal = $m.comments
$textValue = $m.text
$textValue
if ($textValue -ne ''){
$comments += $textValue
}
if ($commentVal){
$exists = $true
}else{
$exists = $false
}
$n++
}
This does work but only for one iteration. i.e. if $search = "comments.comments" $i.$search does not work, but $search = "comments"; $i.$search does work.