kardock
November 16, 2020, 5:02am
1
hi all,
given this string:
$string = ‘“MachineId”: “09f5eb07f057a4499cc1bfafeff95ef0”, “SessionID”: “e489ef09-d1af-4bb1-afd4-37baee89c70c”, “GeoID”: 39, “Ver”: “0.0.0.0”, “C2RClientVer”: “0.0”, “ErrorCode”: 30041, “ErrorType”: “InvalidProductInConfigXml”, “AppVErrorSource”: “”, “ErrorMessage”: “InvalidProductInConfigXml (PerformMSITransitions::HandleStateAction MSI C2R SxS prereq is predicted to fail.)”, “ErrorDetails”: “”, “ContextData”: “OException has occurred.”’
how can i convert this to a hashtable?
thank you!
That appears to be JSON with the missing curly bracket {} wrapper, so you can append the brackets to the string. This is using a string format, which requires escaped brackets:
$string = '"MachineId": "09f5eb07f057a4499cc1bfafeff95ef0", "SessionID": "e489ef09-d1af-4bb1-afd4-37baee89c70c", "GeoID": 39, "Ver": "0.0.0.0", "C2RClientVer": "0.0", "ErrorCode": 30041, "ErrorType": "InvalidProductInConfigXml", "AppVErrorSource": "", "ErrorMessage": "InvalidProductInConfigXml (PerformMSITransitions::HandleStateAction MSI C2R SxS prereq is predicted to fail.)", "ErrorDetails": "", "ContextData": "OException has occurred."'
$myObj = ('{{{0}}}' -f $string) | ConvertFrom-Json
$myObj.MachineId
This creates a PSObject:
PS C:\Users\rasim> $myobj
MachineId : 09f5eb07f057a4499cc1bfafeff95ef0
SessionID : e489ef09-d1af-4bb1-afd4-37baee89c70c
GeoID : 39
Ver : 0.0.0.0
C2RClientVer : 0.0
ErrorCode : 30041
ErrorType : InvalidProductInConfigXml
AppVErrorSource :
ErrorMessage : InvalidProductInConfigXml (PerformMSITransitions::HandleStateAction MSI C2R SxS prereq is predicted to fail.)
ErrorDetails :
ContextData : OException has occurred.
This will allow you to easily parse and get the information rather than manually converting it to a hashtable.
Here’s how I would tackle it.
$string = ‘”MachineId”: “09f5eb07f057a4499cc1bfafeff95ef0”, “SessionID”: “e489ef09-d1af-4bb1-afd4-37baee89c70c”, “GeoID”: 39, “Ver”: “0.0.0.0”, “C2RClientVer”: “0.0”, “ErrorCode”: 30041, “ErrorType”: “InvalidProductInConfigXml”, “AppVErrorSource”: “”, “ErrorMessage”: “InvalidProductInConfigXml (PerformMSITransitions::HandleStateAction MSI C2R SxS prereq is predicted to fail.)”, “ErrorDetails”: “”, “ContextData”: “OException has occurred.”‘
$string = $string -replace '"', '' -replace '”', '' -replace '“','' #remove quotations
$ht = @{} #create new hashtable
foreach ($row in $string -split ", ") {
$pair = $row -split ": "
$ht.($pair[0])= $pair[1]
} #foreach
$ht #return hashtable to screen
Definitely go with Rob’s solution. I didn’t recognize it was JSON. Fun exercise though
[quote quote=271930]Definitely go with Rob’s solution. I didn’t recognize it was JSON. Fun exercise though
[/quote]
Thanks for that breakdown. I’ve been half-heartedly giving up on trying to convert text strings, CSV’s, returned objects to hashtables, but this looks like a good springboard for me! Cheers!
Patrick
Here is another approach
$string = '"MachineId": "09f5eb07f057a4499cc1bfafeff95ef0", "SessionID": "e489ef09-d1af-4bb1-afd4-37baee89c70c", "GeoID": 39, "Ver": "0.0.0.0", "C2RClientVer": "0.0", "ErrorCode": 30041, "ErrorType": "InvalidProductInConfigXml", "AppVErrorSource": "", "ErrorMessage": "InvalidProductInConfigXml (PerformMSITransitions::HandleStateAction MSI C2R SxS prereq is predicted to fail.)", "ErrorDetails": "", "ContextData": "OException has occurred."'
$string -replace ':','=' -split ',' | ConvertFrom-StringData
Name Value
---- -----
"MachineId" "09f5eb07f057a4499cc1bfafeff95ef0"
"SessionID" "e489ef09-d1af-4bb1-afd4-37baee89c70c"
"GeoID" 39
"Ver" "0.0.0.0"
"C2RClientVer" "0.0"
"ErrorCode" 30041
"ErrorType" "InvalidProductInConfigXml"
"AppVErrorSource" ""
"ErrorMessage" "InvalidProductInConfigXml (PerformMSITransitions==HandleStateAction MSI C2R SxS prereq is predicted to fail.)"
"ErrorDetails" ""
"ContextData" "OException has occurred."
And if you wanted to make it a PSCustomObject
$string -replace ':','=' -split ',' |
Foreach-Object -Begin {
$ht = [ordered]@{}
} -Process {
$ht += $_ | ConvertFrom-StringData
} -End {
[PSCustomObject]$ht
}
"MachineId" : "09f5eb07f057a4499cc1bfafeff95ef0"
"SessionID" : "e489ef09-d1af-4bb1-afd4-37baee89c70c"
"GeoID" : 39
"Ver" : "0.0.0.0"
"C2RClientVer" : "0.0"
"ErrorCode" : 30041
"ErrorType" : "InvalidProductInConfigXml"
"AppVErrorSource" : ""
"ErrorMessage" : "InvalidProductInConfigXml (PerformMSITransitions==HandleStateAction MSI C2R SxS prereq is predicted to fail.)"
"ErrorDetails" : ""
"ContextData" : "OException has occurred."