0

I am creating input.json file and i am able to do it but the problem is : this input.json has been created in UCS-E2 BOM encoding format. I think, it is by default encoding format. but i need this in encoding utf format becuase i am getting error while reading through java programming. I have tried couple of solutions but it did not work.

$inputFileJson = @{}
   $inputFileJson.Add("abc", "xyz")
   $inputFileJson.Add("def", "pqr")
   $inputFileJson | ConvertTo-Json -Depth 20 | Out-File 'C:\\Ashish\input.json' -Force

Please help me on this or you can share any article that can help

  • I commend your attention to [Microsoft Docs on `Out-File`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7.1) and specifically note that it does support `-Encoding`. – Jeff Zeitlin May 27 '21 at 15:04
  • I have been already tried this.. it gives UTF-8-BOM format instead of UTF-8 . $inputFileJson | ConvertTo-Json -Depth 20 | Out-File -Encoding utf8 'C:\\Ashish\\input.json' -Force @JeffZeitlin – Ashish Goyanka May 27 '21 at 15:39
  • Read the whole page of the reference I gave you - there is an explicit option to `-Encoding` that suppresses the BOM: `-Encoding utf8NoBOM`. – Jeff Zeitlin May 27 '21 at 15:45
  • You may also find [this SO question](https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom) to be relevant and of interest. – Jeff Zeitlin May 27 '21 at 15:48
  • Yes, you are right . but this -Encoding utf8NoBOM is availabel in powershell 7.1 and i am using powershell 5.1. So, when i am using this coomand in powershell version 5.1 (-Encoding utf8). it creates file in UTF-8 BOM instead if UTF-8. I need this file in UTF-8 encoding format@JeffZeitlin – Ashish Goyanka May 28 '21 at 06:07
  • Uhmmm... Yeah, on further research, it seems you're right with respect to the addition of UTF8NoBOM being after 5.1. You may have to resort to using the .NET class System.IO.File and its methods. – Jeff Zeitlin May 28 '21 at 10:35

0 Answers0