0

I am trying to iterate through some JSON when the exact structure is unknown.

This JSON looks like this:

"object" : { 
     "Item1" : { 
           "property1" : "a"
           "property2" : "b"
           }
       }
      "Item2" : { 
           "property1" : "c"
           "property2" : "d"
           }
       }
  }

The problem is, I don't know what the actual name of Item1 or Item2 is going to be. It is a string of alphanumeric characters that is different on each call.

I have tried

$json_response = $response.object
foreach($item in $json_response) { 
   $id = $item.property1
   Write-Host $id
}

However the $id value never gets set to the value of proprty1. The Write-Host always prints out an empty string.

If I simply do a

Write-Host $json_response

I get something like

@{Item1=; item2=}

I thought this may have been a hash table that would allow me to iterate through it using keys, but there is no Keys property.

Can anyone assist?

Update: Lee_Dailey's response has gotten me further, but still cannot access the properties. With Lee_Dailey's help, I have come up with the following:

        foreach ($item in $response_json.PSobject.Properties) { 
                 $json2 = $item | ConvertTo-Json
                 Write-Host $json2
        }

This creates the following JSON

{
"Value":  {
              "property1":  "a",
              "property2":  "b"
           }
 }

However I still cannot access property1. Doing

$id = json2.Value.property1 
Write-Host $id

results in an empty value. How do I access the properties in the JSON in the $json2 variable?

Update 2. I think I got it working, but I don't know if this is correct or a hack. Updating the code from above to

$json2 = $item | ConvertTo-Json | ConvertFrom-Json 

Seems to allow me to do

$id = $json2.Value.property1

I am not sure where "Value" came from. It is not part of the HTTP response. It also seems odd to convert to JSON and then convert it back, however that seems to expose the properties.

Dave
  • 2,473
  • 2
  • 30
  • 55
  • 3
    there is a hidden property on PoSh objects named `.PSObject` & one of its properties is `.Properties`. that means you can iterate thru `$Var.PSobject.Properties` to find out what is there. – Lee_Dailey May 07 '20 at 19:28
  • Thank you. This has gotten me closer toward the solution. I have updated my question to reflect this. – Dave May 07 '20 at 19:58
  • you are welcome! [*grin*] ///// i cannot run `ConvertFrom-Json` on the text you posted. it gets an `Invalid JSON primitive:` error message. – Lee_Dailey May 07 '20 at 20:12
  • I am not sure what causes that error. I got that same thing using an example directly from Microsoft's site. The JSON I used is just an example of the structure that I am looking at. I didn't want to post the actual JSON, because it contained some sensitive info. At any rate, I have something working (see update #2). I am not sure how correct or elegant it is however. – Dave May 07 '20 at 20:25
  • it works ... that is what counts. [*grin*] thank you for adding it ... i was quite curious. that to/from json step likely means there was something in the `$Items` object that was a tad off ... no telling what, tho. – Lee_Dailey May 07 '20 at 21:13

1 Answers1

1

I'm guessing the json is supposed to be this. These badly structured objects are common. You can loop through properties of "object" using either .psobjects.properties or get-member. Also, in newer versions of powershell, you can convert json to hashtable and use the keys property.

{
  "object": {
    "Item1": {
      "property1": "a",
      "property2": "b"
    }
  },
  "Item2": {
    "property1": "c",
    "property2": "d"
  }
}

See also: Iterating through a JSON file PowerShell

In an ideal world, it would look like this. But it comes out more verbose.

{
  "object": [
    {
      "name": "Item1",
      "value": [
        {
          "name": "property1",
          "value": "a"
        },
        {
          "name": "property2",
          "value": "b"
        }
      ]
    },
    {
      "name": "Item2",
      "value": [
        {
          "name": "property1",
          "value": "c"
        },
        {
          "name": "property2",
          "value": "d"
        }
      ]
    }
  ]
}

$a = cat file.json | convertfrom-json


$a.object

name  value
----  -----
Item1 {@{name=property1; value=a}, @{name=property2; value=b}}
Item2 {@{name=property1; value=c}, @{name=property2; value=d}}


$a.object.value                      

name      value
----      -----
property1 a
property2 b
property1 c
property2 d
js2010
  • 23,033
  • 6
  • 64
  • 66