Modify a JSON file with PowerShell without writing BOM

允我心安 提交于 2019-12-02 02:32:34
Ansgar Wiechers

This has nothing to do with ConvertTo-Json or ConvertFrom-Json. The encoding is defined by the output cmdlet. Out-File defaults to Unicode, Set-Content to ASCII. With each of them the desired encoding can be defined explicitly:

... | Out-File $filePath -Encoding UTF8

or

... | Set-Content $filePath -Encoding UTF8

That will still write a (UTF8) BOM to the output file, but I wouldn't consider UTF-8 encoding without BOM a good practice anyway.

If you want ASCII-encoded output files (no BOM) replace UTF8 with Ascii:

... | Out-File $filePath -Encoding Ascii

or

... | Set-Content $filePath     # Ascii is default encoding for Set-Content
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!