Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Hashtable from JSON

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?

like image 858
Marc Avatar asked Nov 08 '16 19:11

Marc


People also ask

Is JSON a Hashtable?

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).

What does ConvertFrom-JSON do?

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.

Can PowerShell parse JSON?

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.


1 Answers

$json = @{Path="C:\temp"; Filter="*.js"} | ConvertTo-Json  $hashtable = @{}  (ConvertFrom-Json $json).psobject.properties | Foreach { $hashtable[$_.Name] = $_.Value } 

Adapted from PSCustomObject to Hashtable

like image 153
sodawillow Avatar answered Sep 21 '22 15:09

sodawillow