Answered by:
Extracting a hash table from a JSON file with multiple entries

Question
-
Hello,
The Input
I have a [simple] JSON file with application service status information that looks something like:
{ "updated_time":"15:54:11", "services": { "fileserver":"UP", "launcher-rdbms":"DOWN", "launcherdap":"UP" } }
The Goal
I would like to extract the "services" information into the following hash table, that I could then easily parse:
@{ "fileserver" = "UP"; "launcher-rdbms" = "DOWN"; "launcherdap" = "UP" }
The RegEx
I have the following RegEx, which can be used to extract the multiple key=value pairs from the input file:
(?<ServiceName>[^\s@{]+?)=(?<Status>[^};]+)
Thanks,
Larry
Friday, November 8, 2019 10:01 PM
Answers
-
How about this?
$j = @' { "updated_time":"15:54:11", "services": { "fileserver":"UP", "launcher-rdbms":"DOWN", "launcherdap":"UP" } } '@ $p = $j | convertfrom-json $p.services.psobject.properties | foreach{ $_.Name $_.Value }
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Marked as answer by The Silver Canuck Saturday, November 9, 2019 8:28 PM
Saturday, November 9, 2019 3:29 PM
All replies
-
Why not use ConvertFrom-Json to do that?
For example:
$j = @' { "updated_time":"15:54:11", "services": { "fileserver":"UP", "launcher-rdbms":"DOWN", "launcherdap":"UP" } } '@ $p = $j | convertfrom-json $p.services
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Edited by Rich Matheisen [Ex-MVP (retired)] Friday, November 8, 2019 10:12 PM
- Proposed as answer by jrv Friday, November 8, 2019 10:35 PM
Friday, November 8, 2019 10:10 PM -
Hello Rich,
Yeah, I tried that already, but because the resulting object is not a hash table, but rather a System.Management.Automation.PSCustomObject object, I am unable to loop through the results 'keys', like I could if it were a hash table.
If you have any other ideas, I'm all ears.
Thanks,
Larry
Saturday, November 9, 2019 2:52 PM -
How about this?
$j = @' { "updated_time":"15:54:11", "services": { "fileserver":"UP", "launcher-rdbms":"DOWN", "launcherdap":"UP" } } '@ $p = $j | convertfrom-json $p.services.psobject.properties | foreach{ $_.Name $_.Value }
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Marked as answer by The Silver Canuck Saturday, November 9, 2019 8:28 PM
Saturday, November 9, 2019 3:29 PM -
Hello again Rich,
That's it! :)
One question however: How did you go from "$p.services" to "$p.services.psobject", when running the following command:
$p.services | Get-Member
returns:
TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() fileserver NoteProperty string fileserver=UP launcher-rdbms NoteProperty string launcher-rdbms=DOWN launcherdap NoteProperty string launcherdap=UP
In other words, there is no "psobject" property associated with "PSCustomObject"
Thanks again!
Larry
Saturday, November 9, 2019 8:27 PM -
Try $p.services|gm * -force
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Saturday, November 9, 2019 11:57 PM