PowerShell: ConvertTo-Json problem containing special characters

后端 未结 2 1181
旧时难觅i
旧时难觅i 2020-12-17 09:44

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 pas

相关标签:
2条回答
  • 2020-12-17 10:40

    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.

    0 讨论(0)
  • 2020-12-17 10:46

    Try the Unescape() method:

    $jsonfile | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "new.json"
    
    0 讨论(0)
提交回复
热议问题