Skip to content

Instantly share code, notes, and snippets.

@angrychimp
Created December 28, 2021 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angrychimp/50dc18780f6bc15b414a989838c22b77 to your computer and use it in GitHub Desktop.
Save angrychimp/50dc18780f6bc15b414a989838c22b77 to your computer and use it in GitHub Desktop.
Fine JSON errors in JSONL file
<?php
$fin = fopen($argv[1], 'r');
$lno = 0;
while ($line = fgets($fin)) {
$lno++;
try {
$decoded = json_decode($line, true, 512);
if ($decoded === null && json_last_error() !== JSON_ERROR_NONE) {
throw new \Exception("Invalid JSON format");
}
} catch (Exception $e) {
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON error on line $lno: " . json_last_error_msg() . "\n";
} else {
echo "Unknown error on line $lno\n";
}
}
}
echo "parsed $lno lines\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment