I want to get a JSON representation of a Hashtable such as this:
@{Path="C:\temp"; Filter="*.js"}
ConvertTo-Json
results in:
{ "Path": "C:\\temp", "Filter": "*.js" }
However, if you convert that JSON string back with ConvertFrom-Json
you don't get a HashTable but a PSCustomObject.
So how can one reliably serialize the above Hashmap?
The Theory of JSONStandard objects are either a single key and value, or else a collection of keys and values which are equivalent to a hash table in most languages (learn about hash tables in Lua).
The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects.
PowerShell is a great tool to use for manipulating JSON which is used throughout Azure. Have fun scripting! Additional reading: 7.1: ConvertFrom-Json (Microsoft.
$json = @{Path="C:\temp"; Filter="*.js"} | ConvertTo-Json $hashtable = @{} (ConvertFrom-Json $json).psobject.properties | Foreach { $hashtable[$_.Name] = $_.Value }
Adapted from PSCustomObject to Hashtable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With