I have a string in Powershell that is a javascript object. That is, it is a line of text stripped from a Javascript file, for instance, I might have
$theline = "{'prod_id':'123456789','prod_price':'100.00','prod_name':'Creative Software Package','prod_img':'https://mysite/images/123456789?$400x350$','prod_desc':'Software - Package - PC - Win7 - x64'};"
The Javascript elements might have values which contain more than just alphanumeric characters, and I don't think I can guarantee that they won't contain commas.
What I need to do is convert this variable into a Powershell Hash table.
First I was thinking about converting the string into a syntax that would match the ConvertFrom-StringData cmdlet. So the end result would be something like...
$convertedstr = @'
prod_id = 123456789
prod_price = 100.00
prod_name = Creative Software Package
prod_img = https://mysite/images/123456789?$400x350$
prod_desc = Software - Package - PC - Win7 - x64
'@
$table = ConvertFrom-StringData $convertedstr
$table
Name Value
---- -----
prod_desc Software - Package - PC - Win7 - x64
prod_name Creative Software Package
prod_id 123456789
prod_img https://mysite/images/123456789?$400x350$
prod_price 100.00
But I'm not sure how to go about the string replace to get that done. I also found the ConvertFrom-JSON cmdlet, but since I'm on Powershell 2.0 it isn't available to me.
I had tried something like
$theline = $theline -Replace "\{'(\w+)':'(.+)',?\};",'$1 = $2`n'
But it isn't matching it the way I'd like.
$theline
prod_name = Creative Software Package','prod_id':'123456789','prod_price':'100.00`n
I get why the regex is matching what it is, but I'm not sure how to get it to match each "element."
Any ideas for this? I'm open to something that might be easier than this string conversion and a regex replace as well.
Thanks in advance.