POWERSHELL-如何在有条件的情况下访问JSON文件中的多级子元素

can someone please send me solution or link for PowerShell 5 and 7 how can I access child elements if specific condition is fulfilled for JSON file which I have as output.json. I haven't find it on the net. I want to retrieve value of the "children" elements if type element has value FILE and to put that into some list. So final result should be [test1.txt,test2.txt] Thank you!!!

{
   "path": {
      "components": [
         "Packages"
      ],
      "parent": "",
      "name": "Packages",
   },
   "children": {
      "values": [
         {
            "path": {
               "components": [
                  "test1.txt"
               ],
               "parent": "",
               "name": "test1.txt",
            },
            "type": "FILE",
            "size": 405
         },
         {
            "path": {
               "components": [
                  "test2.txt"
               ],
               "parent": "",
               "name": "test2.txt",

            },
            "type": "FILE",
            "size": 409
         },
         {
            "path": {
               "components": [
                  "FOLDER"
               ],
               "parent": "",
               "name": "FOLDER",
            },
            "type": "DIRECTORY",
            "size": 1625
         }
      ]
      "start": 0
   }
}
评论
  • 自恋狂
    自恋狂 回复

    1.)json不正确,我认为这是正确的:

    {
        "path":  {
                     "components":  [
                                        "Packages"
                                    ],
                     "parent":  "",
                     "name":  "Packages"
                 },
        "children":  {
                         "values":  [
                                        {
                                            "path":  {
                                                         "components":  [
                                                                            "test1.txt"
                                                                        ],
                                                         "parent":  "",
                                                         "name":  "test1.txt"
                                                     },
                                            "type":  "FILE",
                                            "size":  405
                                        },
                                        {
                                            "path":  {
                                                         "components":  [
                                                                            "test2.txt"
                                                                        ],
                                                         "parent":  "",
                                                         "name":  "test2.txt"
                                                     },
                                            "type":  "FILE",
                                            "size":  409
                                        },
                                        {
                                            "path":  {
                                                         "components":  [
                                                                            "FOLDER"
                                                                        ],
                                                         "parent":  "",
                                                         "name":  "FOLDER"
                                                     },
                                            "type":  "DIRECTORY",
                                            "size":  1625
                                        }
                                    ],
                         "start":  0
                     }
    }
    

    2.)结构不是绝对清楚的,但是对于您的示例,在我看来,这是正确的解决方案:

    $element = $json | ConvertFrom-Json
    
    $result = @()
    
    $element.children.values | foreach {
                                        if ($_.type -eq 'FILE') { $result += $_.path.name }
                                        }
    
    $result | ConvertTo-Json
    
    

    Be aware, that the used construct $result += $_.path.name is fine if you have up to ~10k items, but for very large items its getting very slow and you need to use an arraylist. https://adamtheautomator.com/powershell-arraylist/