Nick.Mc
Nick.Mc

Reputation: 19205

Powershell JSON transformation removing unicode escape chars without removing literal \n

My issue us similiar to this question:

Json file to powershell and back to json file

When importing and exporting ARM templates in powershell, using Convert-FromJson and Convert-ToJson, introduces unicode escape sequences.

I used the code here to unescape again.

Some example code (mutltiline for clarity):

$armADF = Get-Content -Path $armFile -Raw | ConvertFrom-Json
$armADFString = $armADF | ConvertTo-Json -Depth 50
$armADFString | 
    ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($_) } | 
    Out-File $outputFile

Here's the doco I've been reading for Unescape

Results in the the output file being identical except that all instances of literal \n (that were in the original JSON file) are turned into actual carriage returns. Which breaks the ARM template.

If I don't include the Unescape code, the \n are preserved but so are the unicode characters which also breaks the ARM template.

It seems like I need to pre-escape the \n so when I call Unescape they are turned into nice little \n. I've tried a couple of things like adding this before calling unescape.

$armADFString = $armADFString -replace("\\n","\u000A")

Which does not give me the results I need.

Anyone come across this and solved it? Any accomplished escape artists?

Upvotes: 4

Views: 2284

Answers (1)

Nick.Mc
Nick.Mc

Reputation: 19205

I reread the Unescape doco and noticed that it would also basically remove leading \ characters so I tried this unlikely bit of code:

$armADF = Get-Content -Path $armFile -Raw | ConvertFrom-Json
$armADFString = $armADF | ConvertTo-Json -Depth 50
$armADFString = $armADFString -replace("\\n","\\n")
$armADFString | 
    ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($_) } | 
    Out-File $outputFile

Of course - replacing \\n with \\n makes complete sense :|

More than happy for anyone to pose a more elegant solution.

EDIT: I am deploying ADF ARM templates which are themselves JSON based. TO cut a long story short I also found I needed to add this to stop it unescaping legitimately escaped quotes:

$armADFString = $armADFString -replace('\\"','\\"')

Upvotes: 5

Related Questions