Mit der Windows PowerShell erzeugte JSON-Daten können durch UTF8 mit BOM nicht von PHP eingelesen und verarbeitet werden.
PowerShell Core standardmäßig ohne BOM
Die „PowerShell Core“ ist von diesem „Problem“ angeblich nicht betroffen.
Windows PowerShell: JSON-Datei in UTF8 mit BOM speichern
In der Windows PowerShell wird bei Set-Content und auch bei Out-File mit UTF8 Encoding standardmäßig ein UTF8 mit BOM erzeugt.
$content | ConvertTo-Json -Compress | Set-Content "datei.json" -Encoding UTF8 -Force
Code-Sprache: PowerShell (powershell)
$content | ConvertTo-Json -Compress | Out-File "datei.json" -Encoding UTF8 -Force
Code-Sprache: PHP (php)
Windows PowerShell: JSON-Datei in UTF8 ohne BOM speichern
Über IO.File wird kein BOM erzeugt:
$jsonContent = $content | ConvertTo-Json -Compress
[IO.File]::WriteAllLines("datei.json", $jsonContent)
Code-Sprache: PowerShell (powershell)
PHP: JSON-Daten in Array einlesen
Im Anschluss kann die JSON-Datei ohne Probleme von PHP eingelesen, dekodiert und in ein Array umgewandelt werden:
$jsonContent = json_decode(file_get_contents('data/statistik.json'), true);
Code-Sprache: PHP (php)
Quelle: stackoverflow.com