遍历多维度的 PowerShell JSON 对象

huangapple go评论98阅读模式
英文:

looping through powershell of multiple dimensions json object

问题

我尝试使用foreach和foreach-object,甚至使用convertFrom-json也无法迭代其中的任何值,所以我对这部分有点困惑 - 有什么方法可以迭代每个单元格吗?

我尝试了以下方法,但没有成功:

  1. foreach ($sublist in $data) {
  2. foreach ($item in $sublist){
  3. Write-Host ("the value is :"+$item)
  4. }
  5. }

我想要迭代并获取Timestamp的相应值,即对于test-1234,它是2023-07-11T04:00:24+00:00,那么我应该如何更新循环?

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

是否有任何书籍或在线文章可以供我参考?

如果我有以下数据结构,那么我应该如何迭代:

  1. [
  2. [
  3. "test-1234",
  4. "2023-07-17T04:00:18+00:00",
  5. {
  6. "Code": 8,
  7. "Name": "running"
  8. }
  9. ],
  10. [
  11. "sb-00091",
  12. "2023-07-17T04:00:16+00:00",
  13. {
  14. "Code": 16,
  15. "Name": "running"
  16. }
  17. ]
  18. ]

输出应该如下所示:

  1. a b c d
  2. test-1234 2023-07-11T04:00:24+00:00 8 running
  3. sb-00091 2023-07-11T04:00:21+00:00 16 running
英文:

I have checked with foreach and foreach-object but unable to iterate any value out of it even using convertFrom-json, so I'm little 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) } }

  1. [
  2. [
  3. [
  4. "test-1234",
  5. "2023-07-11T04:00:24+00:00"
  6. ]
  7. ],
  8. [
  9. [
  10. "sb-00091",
  11. "2023-07-11T04:00:21+00:00"
  12. ]
  13. ]
  14. ]

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

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

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

If I have the below data structure then how should I iterate

  1. [
  2. [
  3. "test-1234",
  4. "2023-07-17T04:00:18+00:00",
  5. {
  6. "Code": 8,
  7. "Name": "running"
  8. }
  9. ]
  10. ],
  11. [
  12. [
  13. "sb-00091",
  14. "2023-07-17T04:00:16+00:00",
  15. {
  16. "Code": 16,
  17. "Name": "running"
  18. }
  19. ]
  20. ]
  21. ]

output should be in this way

  1. a b c d
  2. test-1234 2023-07-11T04:00:24+00:00 8 running
  3. sb-00091 2023-07-11T04:00:21+00:00 16 running

答案1

得分: 4

使用建议的解决方案从您之前的问题中提取一个扁平的值列表,然后从每对值创建一个新对象:

  1. $json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $extractedValues = while ($queue.Count) {
  5. $items = $queue.Dequeue()
  6. foreach ($item in $items) {
  7. if ($item -is [System.Collections.ICollection]) {
  8. $queue.Enqueue($item)
  9. continue
  10. }
  11. $item
  12. }
  13. }
  14. $results = for ($i = 0; $i -lt $extractedValues.Count; $i += 2) {
  15. [PSCustomObject]@{
  16. A = $extractedValues[$i]
  17. B = $extractedValues[$i+1]
  18. }
  19. }

这种方法在数据不一致的情况下可能不起作用(例如,如果内部数组中的第二个值可能不是字符串),但您可以修改先前的解决方案以测试给定集合是否具有两个元素并且第一个元素是字符串:

  1. $json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $results = while ($queue.Count) {
  5. $items = $queue.Dequeue()
  6. foreach ($item in $items) {
  7. if ($item -is [System.Collections.ICollection]) {
  8. if ($item.Count -eq 2 -and $item[0] -is [string]){
  9. # we've reached a leaf value, output as an object
  10. [PSCustomObject]@{
  11. A = $item[0]
  12. B = $item[1]
  13. }
  14. }
  15. else {
  16. # otherwise, continue traversing the structure
  17. $queue.Enqueue($item)
  18. }
  19. continue
  20. }
  21. $item
  22. }
  23. }
英文:

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

  1. $json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $extractedValues = while ($queue.Count) {
  5. $items = $queue.Dequeue()
  6. foreach ($item in $items) {
  7. if ($item -is [System.Collections.ICollection]) {
  8. $queue.Enqueue($item)
  9. continue
  10. }
  11. $item
  12. }
  13. }
  14. $results = for ($i = 0; $i -lt $extractedValues; $i += 2) {
  15. [PSCustomObject]@{
  16. A = $extractedValues[$i]
  17. B = $extractedValues[$i+1]
  18. }
  19. }

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:

  1. $json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $results = while ($queue.Count) {
  5. $items = $queue.Dequeue()
  6. foreach ($item in $items) {
  7. if ($item -is [System.Collections.ICollection]) {
  8. if ($item.Count -eq 2 -and $item[0] -is [string]){
  9. # we've reached a leaf value, output as object
  10. [PSCustomObject]@{
  11. A = $item[0]
  12. B = $item[1]
  13. }
  14. }
  15. else {
  16. # otherwise, continue traversing the structure
  17. $queue.Enqueue($item)
  18. }
  19. continue
  20. }
  21. $item
  22. }
  23. }

答案2

得分: 2

类似于Mathias的最后一个示例,但是后续将其强制转换为char的索引递增(65将变成A)。缺点是,如果数组的计数不统一,您最终会得到非规范化的对象或_属性不统一的对象_。

  1. $json = Get-Content path\to\stuff.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $out = [ordered]@{}
  5. while ($queue.Count) {
  6. $items = $queue.Dequeue()
  7. $char = 65
  8. foreach ($item in $items) {
  9. if ($item -isnot [System.Collections.ICollection]) {
  10. $out[[char] $char++] = $item
  11. continue
  12. }
  13. $queue.Enqueue($item)
  14. }
  15. if ($out.Keys) {
  16. [pscustomobject] $out
  17. $out.Clear()
  18. }
  19. }

输出应该如下所示:

  1. A B
  2. - -
  3. test-1234 7/11/2023 1:00:24 AM
  4. sb-00091 7/11/2023 1:00:21 AM
英文:

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.

  1. $json = Get-Content path\to\stuff.json -Raw | ConvertFrom-Json
  2. $queue = [System.Collections.Generic.Queue[object]]::new()
  3. $queue.Enqueue($json)
  4. $out = [ordered]@{}
  5. while ($queue.Count) {
  6. $items = $queue.Dequeue()
  7. $char = 65
  8. foreach ($item in $items) {
  9. if ($item -isnot [System.Collections.ICollection]) {
  10. $out[[char] $char++] = $item
  11. continue
  12. }
  13. $queue.Enqueue($item)
  14. }
  15. if ($out.Keys) {
  16. [pscustomobject] $out
  17. $out.Clear()
  18. }
  19. }

Output should look like:

  1. A B
  2. - -
  3. test-1234 7/11/2023 1:00:24 AM
  4. sb-00091 7/11/2023 1:00:21 AM

huangapple
  • 本文由 发表于 2023年7月14日 01:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定