powershell convert to json 1

powershell convert to json

You could try some string manipulation to get it in an expected JSON format, and then use ConvertFrom-Json to convert it to a PSCustomObject.

Simple Example: (simple because this assumes that these characters being replaced will only be delimiters)

# First, clean up the string.
PS C:\> $mystring = "@{Account='User01';Domain='Domain01';Admin='True'}"
PS C:\> $mystring = $mystring -replace "^@", ""
PS C:\> $mystring = $mystring -replace "=", ":"
PS C:\> $mystring = $mystring -replace ";", ","
PS C:\> $mystring
{Account:'User01',Domain:'Domain01',Admin:'True'}

# Afterwards, convert to PSCustomObject.
PS C:\> $myobject = $mystring | ConvertFrom-Json
PS C:\> $myobject

Account                                 Domain                                  Admin
-------                                 ------                                  -----
User01                                  Domain01                                True

Here is what the above code is Doing:
1. The first line is the string you’re working with.
2. The second line is removing the @ symbol from the beginning of the string.
3. The third line is replacing the = with :
4. The fourth line is replacing the ; with ,
5. The fifth line is just to show you the result of the string manipulation.
6. The sixth line is piping the string to ConvertFrom-Json
7. The seventh line is just to show you the result of the conversion.

I hope this helps.

Similar Posts