PowerShell convert string to json

Question:

In PowerShell I have the following string that I read from file and need to convert to json:

In my PS script I try to do this (simplified):

the result of myStr is:

and not a json I can use. note that the @ sign at the beginning of the string is what I get from the file.

How can I convert it to an object I can use?

Answer:

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)

This can also be converted back to JSON:

Source:

PowerShell convert string to json by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply