24

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"
    }
}
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Maki
  • 439
  • 1
  • 6
  • 17

3 Answers3

50

Try the Unescape() method:

$jsonfile | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "new.json"
Daniel Auger
  • 12,535
  • 5
  • 52
  • 73
Vladimir Dronov
  • 1,182
  • 9
  • 11
  • 12
    Warning: this removes ALL escapes, including some that might be needed in your JSON, e.g. escaped quote characters: `"expression": "My name is \"joe\""`. This leaves you with broken JSON... – Charlie Joynt Jan 05 '18 at 16:58
  • 2
    I found the following answer to be most satisfying - https://stackoverflow.com/a/47779605/2778027 – Stringfellow Jan 11 '19 at 01:36
8

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.

Community
  • 1
  • 1
Booga Roo
  • 1,665
  • 1
  • 21
  • 30
  • Thanks! I think i am going to work on something to convert the password after its convertedfrom-json... Will update as soon i get something... – Maki Mar 30 '15 at 14:10
  • I just re-read your post and realize it's not a real problem anymore. Turn out to be a cosmetic issue for now. Even thought the password is in unicode, It works.. So I am not going to worry about converting it for now.. – Maki Mar 30 '15 at 14:57
0

Tested with PowerShell 7.2 and appears unicode and other special characters are converted successfully. Not to mention indentation also appears improved.

Get-Tek
  • 51
  • 3