Skip to content

Instantly share code, notes, and snippets.

@mrunkel
Created April 11, 2023 07:24
Show Gist options
  • Save mrunkel/bc3084d89ea882ef4549cfb3ec37576c to your computer and use it in GitHub Desktop.
Save mrunkel/bc3084d89ea882ef4549cfb3ec37576c to your computer and use it in GitHub Desktop.
Small PHP function to see if a text blob is json or not
private function isJson($data): bool
{
// check that the first character is { or [
if (in_array(substr($data, 0, 1), ['{', '['])) {
// if that worked, try to process as json
// no errors, return true.
$json = json_decode($data);
return json_last_error() === JSON_ERROR_NONE;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment