skip to Main Content

I have checked with foreach and foreach-object but unable to iterate any value out of it even using convertFrom-json, so I’m liitle bit clueless on this part – any idea how to iterate though every cell of it ?
I have tried with the below but it didn’t work out

foreach ($sublist in $data) { foreach ($item in $sublist){ Write-Host
("the value is :"+$item) } }

[
    [
        [
            "test-1234",
            "2023-07-11T04:00:24+00:00"
        ]
    ],
    [
        [
            "sb-00091",
            "2023-07-11T04:00:21+00:00"
        ]
    ]
]

I would like to iterate through and would like to get the corresponding value of Timestamp i.e. for test-1234 it is 2023-07-11T04:00:24+00:00 , then how should I need to update the loop

a                            b
test-1234                  2023-07-11T04:00:24+00:00
sb-00091                   2023-07-11T04:00:21+00:00

Is there any book or online article available if I can go through it

2

Answers


  1. Use the suggested solution from your previous question to extract a flat list of values, then create a new object from each pair:

    $json = Get-Content pathtojson.json -Raw | ConvertFrom-Json
    $queue = [System.Collections.Generic.Queue[object]]::new()
    $queue.Enqueue($json)
    
    $extractedValues = while ($queue.Count) {
        $items = $queue.Dequeue()
        foreach ($item in $items) {
            if ($item -is [System.Collections.ICollection]) {
                $queue.Enqueue($item)
                continue
            }
            $item
        }
    }
    
    $results = for ($i = 0; $i -lt $extractedValues; $i += 2) {
      [PSCustomObject]@{
        A = $extractedValues[$i]
        B = $extractedValues[$i+1]
      }
    }
    

    This approach might not work if the data isn’t uniform (eg. if the second value in the inner array might not be a string), but you could modify the previous solution to test for whether a given collection has a count of two and a string in the first slot:

    $json = Get-Content pathtojson.json -Raw | ConvertFrom-Json
    $queue = [System.Collections.Generic.Queue[object]]::new()
    $queue.Enqueue($json)
    
    $results = while ($queue.Count) {
        $items = $queue.Dequeue()
        foreach ($item in $items) {
            if ($item -is [System.Collections.ICollection]) {
                if ($item.Count -eq 2 -and $item[0] -is [string]){
                    # we've reached a leaf value, output as object
                    [PSCustomObject]@{
                        A = $item[0]
                        B = $item[1]
                    }
                }
                else {
                    # otherwise, continue traversing the structure
                    $queue.Enqueue($item)
                }
                continue
            }
            $item
        }
    }
    
    Login or Signup to reply.
  2. Similar approach to Mathias’s last example but incrementing an index that is coerced into a char later (65 would be A). Downside is that if arrays don’t have uniform counts you would end up with not normalized objects or objects not having uniform properties.

    $json = Get-Content pathtostuff.json -Raw | ConvertFrom-Json
    $queue = [System.Collections.Generic.Queue[object]]::new()
    $queue.Enqueue($json)
    
    $out = [ordered]@{}
    while ($queue.Count) {
        $items = $queue.Dequeue()
        $char = 65
    
        foreach ($item in $items) {
            if ($item -isnot [System.Collections.ICollection]) {
                $out[[char] $char++] = $item
                continue
            }
    
            $queue.Enqueue($item)
        }
    
        if ($out.Keys) {
            [pscustomobject] $out
            $out.Clear()
        }
    }
    

    Output should look like:

    A         B
    -         -
    test-1234 7/11/2023 1:00:24 AM
    sb-00091  7/11/2023 1:00:21 AM
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search