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

6
  • You're looking to write a function that can take an argument, and quote it to be read as a proper JSON object? Commented Mar 2, 2022 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. Commented Mar 2, 2022 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 Commented Mar 2, 2022 at 23:20
  • If you're calling the PowerShell CLI from PowerShell, you may be seeing this longstanding bug: 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. Commented Mar 2, 2022 at 23:42
  • Hi @mklement0 thanks for that, unfortunately, it is passed in from arm template, because it is a custom script extension.. Commented Mar 3, 2022 at 5:23

2 Answers 2

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 '\}|(?<!\}),', '"$&'
Sign up to request clarification or add additional context in comments.

Comments

0

eventually hacked it by using replace

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.