PowerShell:包含特殊字符的转换器- json问题

时间:2023-01-28 00:22:03

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.

我正在编写一个对JSON文件进行修改的脚本,但是当这个文件被转换回JSON时,它会扩展一些特殊的字符。

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

例如,JSON文件包含带有“&”的密码。快速复制这个问题的方法是使用以下命令:

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

PS>“Password&123”| Convertto-Json输出为:“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

——这里是一个简化的JSON文件示例

{
    "Server1":
    {
        "username":"root",
        "password":"Password&dfdf"
    },
    "Server2":
    {
        "username":"admin",
        "password":"Password&1234"
    }
}

2 个解决方案

#1


2  

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

这是由Convertto-Json的自动字符转义特性导致的,它影响几个符号,如<>\'&

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

ConvertFrom-Json将正确读取转义字符。用你的例子:

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:

示例代码会生成一个转义字符的文件,但是ConvertFrom-Json可以将它读回原始密码。见下文:

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

如果需要不转义地存储密码,则可能需要一些更高级的工作。有关转换Unicode字符串以避开ascii字符串的线程。

Alternatively, avoid affected characters if possible.

或者,尽可能避免受影响的字符。

#2


16  

Try the Unescape() method:

尝试Unescape()方法:

$jsonfile | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "new.json"

#1


2  

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

这是由Convertto-Json的自动字符转义特性导致的,它影响几个符号,如<>\'&

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

ConvertFrom-Json将正确读取转义字符。用你的例子:

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:

示例代码会生成一个转义字符的文件,但是ConvertFrom-Json可以将它读回原始密码。见下文:

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

如果需要不转义地存储密码,则可能需要一些更高级的工作。有关转换Unicode字符串以避开ascii字符串的线程。

Alternatively, avoid affected characters if possible.

或者,尽可能避免受影响的字符。

#2


16  

Try the Unescape() method:

尝试Unescape()方法:

$jsonfile | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File "new.json"