17

I am writing a script for my site that utilizes a JSON configuration file. The JSON is similar to the following:

"Groups": {
    "GroupOne": {
        "NamingFilter": {
            "Not":"<SITE>-MJ*",
            "Has":"*WM,*WT"
        }
    },
    "GroupTwo": {
        "NamingFilter": {
            "Has":"<SITE>-MJ*, *WC,*WL"
        }
    },
    "GroupThree": {
        "NamingFilter": {
            "Not":"<SITE>-MJ*",
            "Has":"*WI"
        }
    }
}

To convert the object to something PowerShell can read, I use ConvertFrom-Json which converts it to type PSCustomObject.

I am at a point where I have to iterate over the Groups and get each group name to output them and their corresponding index within the configuration's Groups object. That is,

1. GroupOne
2. GroupTwo
3. GroupThree

The furthest I have gotten is:

foreach ($group in $configObject.Groups) {
    $group
}

And all this does is output something that looks like PowerShell array notation:

@{GroupOne=; GroupTwo=; GroupThree=;}

Is this even possible with a PSCustomObject type? I primarily code in JavaScript so perhaps I am oversimplifying (or overcomplicating it) the issue since this would be relatively easy.

1
  • Just a note - the JSON shown in the example needs a open "{" and closed "}" curly bracket around it to make it valid. Dec 21, 2022 at 12:06

2 Answers 2

31

Solution

I'm not sure how early of a version you can do this with, but it works for me in PowerShell 5.1... Feel free to give it a try in earlier versions:

$ipinfo = Invoke-WebRequest 'http://ipinfo.io/json' -UseBasicParsing | ConvertFrom-Json
foreach ($info in $ipinfo.PSObject.Properties) {
    $info.Name
    $info.Value
    '--' # <-- Seeing this double hash proves we're iterating fully.
}

Outputs

ip
1.2.3.4
--
hostname
No Hostname
--
city
Denton
--
region
Texas
--
country
US
--
loc
33.2148,-97.1331
--
org
AS589 University of North Texas
--
postal
76203
--

This is all done with PowerShell 4.0; ConvertTo-Json and ConvertFrom-Json were introduced in PowerShell 3.0; I haven't tested PowerShell 3.0 or 5.0.


Another Example

Try it for yourself with another example:

$ipinfo = ConvertFrom-Json (Invoke-WebRequest 'http://ipinfo.io/json' -UseBasicParsing)
foreach ($info in ($ipinfo.PSObject.Members | ?{ $_.MemberType -eq 'NoteProperty'})) {
    $info.Name
    $info.Value
    '--'
}

Outputs

ip
1.2.3.4
--
hostname
No Hostname
--
city
Denton
--
region
Texas
--
country
US
--
loc
33.2148,-97.1331
--
org
AS589 University of North Texas
--
postal
76203
--

This and other PowerShell looping are available on my blog.

12

I think I might have just figured it out thanks to the blog post Converting PsCustomObject To/From Hashtables.

Using Get-Member and then -MemberType exposes each Group and then you just pull the:

foreach ($group in $configObject.Groups) {
    $groupName = $($group | Get-Member -MemberType *Property).Name
}

Outputs:

GroupOne
GroupTwo
GroupThree

I'm open to any other methods though.

Update

I've found another way, but the only drawback is it doesn't use the fancy ConvertFrom-Json CmdLet. Instead it goes straight to the .NET library and uses its deserializer and converts it to a HashTable. This completely avoids having to muddle around with the PSCustomObject. IMO hashtables are a whole lot easier to work with.

$JSON = Get-Content -Path path/to.json -Raw
$HT = (New-Object System.Web.Script.Serialization.JavaScriptSerializer).Deserialize($JSON, [System.Collections.Hashtable])
$HT.Groups.GetEnumerator() | ForEach-Object {
    Write-Host "$($_.Key) : $($_.Value)"
}
2
  • I haven't figured out the actual solution, but your solution doesn't actually enumerate through the object. This variation of your code: foreach ($group in $configObject.Groups) {'--'; ($group | Get-Member -MemberType *Property).Name } will only output the -- once; meaning it only passes through the loop one time. POSH 4.0
    – VertigoRay
    Nov 18, 2015 at 22:12
  • I found an posted the solution. Hope it helps others. :)
    – VertigoRay
    Nov 18, 2015 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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