vel
vel

Reputation: 1210

POWERSHELL - How to access multilevel child elements in JSON file with condtion

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

Upvotes: 1

Views: 1310

Answers (1)

Farbkreis
Farbkreis

Reputation: 644

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

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. https://adamtheautomator.com/powershell-arraylist/

Upvotes: 1

Related Questions