I kinda need help retrieving deep values from HashTable.
My Situation: I wrote a C# executable which gives me data in JSON format that I stored in a variable $Result. I'm able to fetch and process all fields of $Result except nested HashTable called Properties and its keys (Locations). I just need to list the cities which are mentioned in the HashTable called Properties
At the minute I'm able to get the Locations Key as shown below
PS C:\WINDOWS\system32> $Result.Properties.Locations
City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
PS C:\WINDOWS\system32>
Expected Output:
NewYork
Seattle
Miami
SanJose
Properties Hashtable
[Properties]@
{
accountExpires=Never;
AccountLockedOut=False;
mail=JamesBond@internationalspy.com;
cn=Tom Cruise;
codePage=0;
countryCode=0;
displayName=Tom Cruise;
employeeID=743355;
Enabled=True;
givenName=Tom;
instanceType=4;
lastLogoff=12/31/1600 4:00:00 PM;
lastLogon=3/27/2020 7:52:36 AM;
lastLogonTimestamp=5/25/2020 12:47:07 PM;
lockoutTime=12/31/1600 4:00:00 PM;
logonCount=4;
mail=jamesbond@InternalActor.com;
Locations=City=NewYork,Country=USA,Year=2001;City=Seattle,Country=USA,Year=2002;City=Miami,Country=USA,Year=2010;City=SanJose,Country=USA,Year=2009
}
I need to get the fetch the list of cities which are nested with the above Hashtable. I just want the list of cities so that I can further apply my business logic and proceed further.
Here's some more alternatives for you:
split and replace:
($Result.Properties.Locations -split '[;,]' | Where-Object { $_ -match '^City=' }) -replace '^City='
split and ConvertFrom-StringData
($Result.Properties.Locations -split '[;,]' | ConvertFrom-StringData).City
iterate over matches
$match = ([regex]'(?i)City=([^,]+)').Match($Result.Properties.Locations)
while ($match.Success) {
$match.Groups[1].Value
$match = $match.NextMatch()
}
All result in
NewYork Seattle Miami SanJose