Escaping JSON

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Monte Hazboun Monte Hazboun 5 months, 1 week ago.

  • Author
    Posts
  • #60292
    Profile photo of Anthony
    Anthony
    Participant

    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?

  • #60328
    Profile photo of Monte Hazboun
    Monte Hazboun
    Participant

    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($_) }
    

You must be logged in to reply to this topic.