1

Here's an example blob of JSON:

{
  "inventory": {
    "trucks": {
      "wheels": {
        "count": 4,
        "brand": "fireyear"
      },
      "engine": {
        "valveCount": 3
      }
    },
    "planets": {
      "ringed": [
        "saturn"
      ],
      "questionable status": [
        "pluto"
      ]
    }
  }
}

I want to write a powershell function that takes a node as an argument, and determines if it's object-like or not. If it isn't, it does something with the value. Otherwise, it calls itself recursively with each value.

Something like this workflow:

PS> $x=(Get-Content -Path big-blob-of-json.json | ConvertFrom-Json)
PS> f("", $x)

where the empty-string in the call to the json-processing-function indicates that we're at the root.

All the docs I've looked at so far show how to convert a hashobject into a PSCustomObject, but from what I can tell those work with known keys. In my case the keys are completely arbitrary and unknown in advance, but are of interest.

Eric
  • 2,115
  • 2
  • 20
  • 29
  • 1
    There are many examples already on how to traverse a json recursively in this site, have you tried searching? – Santiago Squarzon Mar 15 '23 at 19:12
  • Here's one of the many similar (identical?) questions... https://stackoverflow.com/questions/27195254/dynamically-get-pscustomobject-property-and-values - the crucial part is that you can use ```$x.PSObject.Properties``` to get the list of properties which you can then access programmatically via their ```Name``` and ```Value```. – mclayton Mar 15 '23 at 19:28
  • On a Json level: https://stackoverflow.com/a/74352656/1701026, on a PowerShell level: https://stackoverflow.com/a/74337640/1701026 – iRon Mar 15 '23 at 19:54

0 Answers0