fdo fdo - 5 months ago 229
JSON Question

PowerShell: ConvertTo-Json problen 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"
}
}

Answer

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.

Comments