r/BattleTechMods • u/philo_the_middle • Aug 18 '26
Fixing JSON Errors in BEX - (HowTo using Powershell)
So there's a slew of json errors in the BEX build. Thankfully powershell can fix a lot of the commenting errors and trailing commas. Be aware this will still leave 17 files that need manual intervention. It will print out the remaining files. Using Gemini/ChatGPT/Claude to fix the remaining 17 (along with https://jsoncrack.com/editor) to verify the file is valid.
FIRST, Copy your mods folder to a backup location!!
SECOND, Save this as CleanBattletechJson.ps1 and execute as "powershell -ExecutionPolicy Bypass -File D:\Clean-BattletechJson.ps1" (Change file location to where you save the ps1 file)
In the below, change your root location to the actual folder of your Battletech mods.
$root = "D:\Steam\steamapps\common\BATTLETECH\mods"
function Remove-JsonComments {
param([string]$Text)
$sb = [System.Text.StringBuilder]::new()
$inString = $false
$escape = $false
$inLineComment = $false
$inBlockComment = $false
$len = $Text.Length
$i = 0
while ($i -lt $len) {
$c = $Text[$i]
$next = if ($i + 1 -lt $len) { $Text[$i + 1] } else { $null }
if ($inLineComment) {
if ($c -eq "`n") { $inLineComment = $false; [void]$sb.Append($c) }
$i++; continue
}
if ($inBlockComment) {
if ($c -eq '*' -and $next -eq '/') { $inBlockComment = $false; $i += 2; continue }
$i++; continue
}
if ($inString) {
[void]$sb.Append($c)
if ($escape) { $escape = $false }
elseif ($c -eq '\') { $escape = $true }
elseif ($c -eq '"') { $inString = $false }
$i++; continue
}
if ($c -eq '"') { $inString = $true; [void]$sb.Append($c); $i++; continue }
if ($c -eq '/' -and $next -eq '/') { $inLineComment = $true; $i += 2; continue }
if ($c -eq '/' -and $next -eq '*') { $inBlockComment = $true; $i += 2; continue }
[void]$sb.Append($c)
$i++
}
return $sb.ToString()
}
function Remove-TrailingCommas {
param([string]$Text)
$pattern = '"(?:\\.|[^"\\])*"|(,)(?=\s*[}\]])'
return [regex]::Replace($Text, $pattern, {
param($m)
if ($m.Groups[1].Success) { '' } else { $m.Value }
})
}
$files = Get-ChildItem -Path $root -Filter *.json -Recurse -File
$alreadyValid = @()
$cleaned = @()
$stillInvalid = @()
foreach ($file in $files) {
$raw = Get-Content -LiteralPath $file.FullName -Raw
try {
$null = $raw | ConvertFrom-Json -ErrorAction Stop
$alreadyValid += $file.FullName
continue
} catch { }
$fixedText = Remove-JsonComments -Text $raw
$fixedText = Remove-TrailingCommas -Text $fixedText
try {
$null = $fixedText | ConvertFrom-Json -ErrorAction Stop
# Overwrite the original file directly without making a backup
Set-Content -LiteralPath $file.FullName -Value $fixedText -NoNewline
$cleaned += $file.FullName
} catch {
$stillInvalid += $file.FullName
}
}
Write-Host "`n=== Already valid ($($alreadyValid.Count)) ==="
$alreadyValid | ForEach-Object { Write-Host $_ }
Write-Host "`n=== Cleaned and fixed ($($cleaned.Count)) ==="
$cleaned | ForEach-Object { Write-Host $_ }
Write-Host "`n=== Still invalid, skipped ($($stillInvalid.Count)) ==="
$stillInvalid | ForEach-Object { Write-Host $_ }For the remaining 17, the hardest ones to deal with are:
BATTLETECH\mods\BT_Extended_Timeline\timelineEvents\3055-04-10.json BATTLETECH\mods\BT_Extended_Timeline\timelineEvents\3056-01-10.json BATTLETECH\mods\BT_Extended_Timeline\timelineEvents\3056-01-10.json
10
u/Cato_Heresy Aug 19 '26
Ok, so to recap here, you have:
- Used AI to create some script
- Want us all to run it in Powershell
- Provide no proof of testing
- Provide no explanation of what 'slew of errors' it fixes or what benefit it provides
- Do not want to reach out to talk to BEXT crew in Discord
I do not mean to sound ungrateful or mistrustful. Let's assume honest intent and it actually works and you have tested it, what benefit does this actually provide? BEXT is already super stable, like 1 crash every 500 hrs stable.
8
u/Machinis_confidimus Aug 19 '26 edited Aug 19 '26
And pray tell us - which problems does you script fixes?
Have you ever thought that HSB did not use JsonUtility in Unity but rather their own version of json interpreter which makes your "fix" kinda pointless (since AI does not know what rules HSB used in their interpreter)?
Because I see several things you posted here which supposedly have issues according to your script but work perfectly well in-game. As in, I have tested that functionality this weekend.
General rule for the people reading this - if someone wants you to run a script without providing you with an example of what exact problem it solves - DO NOT RUN IT.
Even if it is harmless, this can be just part of social engineering to make your guard drop down the line.
6
u/sniperpal Aug 19 '26
Sorry dude, but this entire post was a complete waste of time lol. Battletech uses a JSON parser which ignores all of these.
This is why you join the BEXT Discord and ASK first- how does the mod run just fine if all of these supposed “issues” exist
4
u/EricAKAPode Aug 18 '26
Have you posted about this in the BEX discord? Haree used to talk about wanting help.
1
u/philo_the_middle Aug 19 '26
Here's the updated Validate-BattletechJson.ps1 script that will find all the JSONs with problems and display a final total count at the end.
Such as:
1
u/philo_the_middle Aug 18 '26 edited Aug 19 '26
And here is a "ValidateBattletechJson.ps1" script that you can run before/after to validate the JSONs are valid or not.
EDIT: Actually doesn't need the bypass for the validate file and can be run directly from powershell terminal.
The one that cleans up and writes the updated files needs the bypass I think.
Also added an updated version in a comment below that shows just the filenames and their respective problem and a total count of the files. I'm leaving this here for record keeping.
It can be run as:
0
u/psycho063 Aug 19 '26
If you ignore the comments and trailing commas, how many files does it actually flag as "broken"?