Created
December 28, 2021 17:08
-
-
Save angrychimp/50dc18780f6bc15b414a989838c22b77 to your computer and use it in GitHub Desktop.
Fine JSON errors in JSONL file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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