1

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

1.) The json is incorrect, I assumt that this one is the correct one:

{
    "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
                 }
}

2020欧洲杯时间表2.) The structure is not absolute clear, but for your example this seems to me to be the correct solution:

$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.

| improve this answer | |
  • Thanks. this is great. If I may ask you just one more question - my IDE is suggesting me to use ForEach-Object instead of foreach? Is it Ok? Thanks – AndreyDonald May 24 at 0:42
  • yes thats fine. – user3898488 May 24 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.