Monday, April 3, 2023

Convert Json string to hashtable


Powershell stores json string psobject type  
1. Define empty Hashtable
2. Iterate each property of psobject 
3. Add value of psobject into Hashtable with name property as key/

Here is the script for convert JSON string to Hashtable.

```sh

      
 function  ConvertJson-ToHashTable(){
    [CmdletBinding()]
            Param(
            [Parameter(Mandatory=$true,ValueFromPipeline=$true )]
            [psobject] $inputObj)
    $hash = @{}
    $inputObj.psobject.properties | foreach{$hash[$_.Name]= [string[]]$_.Value}
    return  $hash
}
```

No comments:

Post a Comment