Author Posts

December 16, 2016 at 1:42 am

Hi,

I'm currently trying to write a JSON document but am having some issues with convertto-json escaping special characters mainly the characters

Data

Edit: pre tags really don't like the above so put it in a gist instead

Result:

$blah | ConvertTo-Json
[
    "\u003cscript\u003e\n",
    "cfn-init.exe -v -s ",
    "   -r WindowsServer2012R2 ",
    "   --region ",
    "\n",
    "\u003c/script\u003e"
]

\u003 in JSON is basically the < and convertfrom-json has no issues reading it back but the JSON document needs to be basically what's in the $blah variable. Normal powershell escaping using ` doesn't appear to be working. Any ideas?

December 16, 2016 at 2:54 pm

The regex .NET class has an unescape method that you can make use of. See if this works for you.

$blah | ConvertTo-Json | % { [regex]::Unescape($_) }