Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: ConvertTo-Json problem containing special characters

I am writing a script to make changes to a JSON file but when the file is converted back to JSON it expands special characters.

For example the JSON File contain passwords with "&". A quick way to replicate the problem is using following command:

PS> "Password&123" | Convertto-Json output is:"Password\u0026123"

##Here is how I import the JSON FILE:
$jsonfile = (Get-Content .\example.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
##Exporting JSON FILE without modifying it.
$jsonfile  | ConvertTo-Json |Out-File "new.json"

--here is an example of simplified JSON FILE

{
    "Server1":
    {
        "username":"root",
        "password":"Password&dfdf"
    },
    "Server2":
    {
        "username":"admin",
        "password":"Password&1234"
    }
}
like image 734
Maki Avatar asked Mar 27 '15 17:03

Maki


2 Answers

Try the Unescape() method:

$jsonfile | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "new.json"
like image 119
Vladimir Dronov Avatar answered Oct 29 '22 18:10

Vladimir Dronov


This is caused by the automatic character escape feature of Convertto-Json and it affects several symbols such as <>\'&

ConvertFrom-Json will read the escaped characters properly. Using your example:

PS C:\> {"Password\u0026123"} | ConvertFrom-Json
Password&123

And your example code results in a file that has escaped characters, but ConvertFrom-Json can read it back to the original passwords. See below:

PS C:\> (Get-Content .\example.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

PS C:\> (Get-Content .\new.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

If you need the passwords to be stored unescaped, some fancier work may be needed. See this thread about Converting Unicode strings to escaped ascii strings

Alternatively, avoid affected characters if possible.

like image 8
Booga Roo Avatar answered Oct 29 '22 19:10

Booga Roo