none
Convert String to Hashtable

    Question

  • Hi,

    i have the following string, would like to convert it to HashTable

    $tempString = "{[R,RED],[G,GREEN],[B,BLUE]}"

    i can do string replace and splits and convert it to hashtable, but was wondering if there was another quick way

    thanks

    exchposh

    Thursday, September 26, 2013 3:46 PM

Answers

  • Not without at least some string manipulation on your part.  There is a handy ConvertFrom-StringData cmdlet that will convert a string to a hashtable, but the string has to be formatted with "Name=Value" pairs, one on each line:

    $stringData = @'
    R = Red
    G = Green
    B = Blue
    '@
    
    $stringData | ConvertFrom-StringData
    
    
    Name                           Value
    ----                           -----
    G                              Green    
    R                              Red             
    B                              Blue

    Thursday, September 26, 2013 4:33 PM
  • I ended up writing a function

    Function ConvertString-toHash{
    Param($tmpStr)
    $tmpStr = $tmpStr.Replace('}','')
    $tmpStr = $tmpStr.Replace('{','')
    $tmpStr = $tmpStr.Replace('],','|')
    $tmpStr = $tmpStr.Replace(']','')
    $tmpStr = $tmpStr.Replace('[','')
    $tmpStr = $tmpStr.Replace('[','')
    $tmpStr = $tmpStr.Replace(',','=')
    $tmpArr = $tmpStr.Split('|')
    $tmpHash = @{}
    foreach($tmpArrEntry in $tmpArr)
    {
    	$tmpKey = $tmpArrEntry.Split('=')[0]
    	$tmpValue = $tmpArrEntry.Split('=')[1]
    	$tmpHash.Add($tmpKey.Trim(),$tmpValue.Trim())
    }
    Write-output $tmpHash
    }

    Thursday, September 26, 2013 4:47 PM

All replies

  • Not without at least some string manipulation on your part.  There is a handy ConvertFrom-StringData cmdlet that will convert a string to a hashtable, but the string has to be formatted with "Name=Value" pairs, one on each line:

    $stringData = @'
    R = Red
    G = Green
    B = Blue
    '@
    
    $stringData | ConvertFrom-StringData
    
    
    Name                           Value
    ----                           -----
    G                              Green    
    R                              Red             
    B                              Blue

    Thursday, September 26, 2013 4:33 PM
  • I ended up writing a function

    Function ConvertString-toHash{
    Param($tmpStr)
    $tmpStr = $tmpStr.Replace('}','')
    $tmpStr = $tmpStr.Replace('{','')
    $tmpStr = $tmpStr.Replace('],','|')
    $tmpStr = $tmpStr.Replace(']','')
    $tmpStr = $tmpStr.Replace('[','')
    $tmpStr = $tmpStr.Replace('[','')
    $tmpStr = $tmpStr.Replace(',','=')
    $tmpArr = $tmpStr.Split('|')
    $tmpHash = @{}
    foreach($tmpArrEntry in $tmpArr)
    {
    	$tmpKey = $tmpArrEntry.Split('=')[0]
    	$tmpValue = $tmpArrEntry.Split('=')[1]
    	$tmpHash.Add($tmpKey.Trim(),$tmpValue.Trim())
    }
    Write-output $tmpHash
    }

    Thursday, September 26, 2013 4:47 PM
  • Here's a way to do it with a Regular Expression (though, being a regular expression, it's a pain in the butt to read):

    $tempString = "{[R,RED],[G,GREEN],[B,BLUE]}"
    $pattern = '\[(?<Key>[^,\]]+),(?<Value>[^\]]+)\]'
    
    $hashTable = @{}
    
    foreach ($match in [regex]::Matches($tempString, $pattern))
    {
        $hashTable[$match.Groups['Key'].Value] = $match.Groups['Value'].Value
    }
    
    $hashTable
    

    Thursday, September 26, 2013 4:49 PM
  • Hi,

    just for the fun of it (knowing that this is not what was asked for). Here is a one liner using replace split and join:

    ("{[R,RED],[G,GREEN],[B,BLUE]}" -split '\],\[' -replace '[{|}|\]|\[]','').Replace(",","=") -join "`n" | 
      ConvertFrom-StringData
    
    



    • Edited by Dirk_74 Thursday, September 26, 2013 11:04 PM
    Thursday, September 26, 2013 10:40 PM