1

I have this string

[{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]

i'm wondering how can I write a function to convert it to

[{"listenport":"443","connectaddress":"10.1.10.20","connectport":"443","firewallrulename":"port443","direction":"Inbound","action":"Allow","protocol":"TCP"},{"listenport":"80","connectaddress":"10.1.10.20","connectport":"80","firewallrulename":"port80","direction":"Inbound","action":"Allow","protocol":"TCP"}]

I have tried to use insert and indexof , but couldn't figure out how to do for an entire string

Roger Chen
  • 233
  • 3
  • 15
  • You're looking to write a function that can take an argument, and quote it to be read as a proper JSON object? – Abraham Zinala Mar 02 '22 at 22:26
  • 3
    Where do you get the source string? That looks like malformed JSON, so fixing the source to return valid JSON might be your best bet. – vonPryz Mar 02 '22 at 22:27
  • hi Guys, the reason for that json input is this is used as a custom script extension, and i'm trying to pass in a arm template parameter, in order to use concat[], it has to be converted to a string from array. and even worse, when doing powershell -filelocation -parameter, it changes all the input, so it removes all the double quotes originally has. driving me crazy – Roger Chen Mar 02 '22 at 23:20
  • If you're calling the PowerShell CLI _from PowerShell_, you may be seeing this longstanding bug: https://stackoverflow.com/a/66837948/45375. However, you can avoid it by passing the code to execute as a _script block_ (`{ ... }`) and arguments to it via `-Args`. That said, there's rarely a good reason to call the PowerShell CLI _from inside PowerShell_. – mklement0 Mar 02 '22 at 23:42
  • Hi @mklement0 thanks for that, unfortunately, it is passed in from arm template, because it is a custom script extension.. – Roger Chen Mar 03 '22 at 05:23
  • I see. I'm not familiar with ARM templates, but if the problem is ultimately what I suspect it is (I'm not sure), you can try to replace `"` instances with `\"` in the argument passed to the PowerShell CLI, using the [`replace` function](https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-string#replace). – mklement0 Mar 03 '22 at 08:03

2 Answers2

3

If you really have to work with this format and cannot produce well-formed JSON to begin with, at least in your sample input both the property names and values are composed only of characters that are either . or fall into the \w regex category, so a single -replace operation is possible:

@'
[{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]
'@ -replace '[\w.]+', '"$&"'

The result is well-formed JSON, which you can pipe to ConvertFrom-Json for OO processing in PowerShell.


If you can only assume that the property names are composed of only \w characters:

@'
[{listenport:443,connectaddress:10.1.10.20,connectport:443,firewallrulename:port443,direction:Inbound,action:Allow,protocol:TCP},{listenport:80,connectaddress:10.1.10.20,connectport:80,firewallrulename:port80,direction:Inbound,action:Allow,protocol:TCP}]
'@ -replace '(\w+):', '"$1":"' -replace '\}|(?<!\}),', '"$&'
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

eventually hacked it by using replace

$proxyinfosjson = $proxyinfosjson.Replace(',', '","').Replace('{', '{"').Replace('}', '"}').replace(':', '":"').Replace('}"', '}').Replace('"{', '{')

so ugly.. not proud of it.. but works..

mklement0
  • 382,024
  • 64
  • 607
  • 775
Roger Chen
  • 233
  • 3
  • 15