The fastest way is to "maybe decode" the possible JSON string
Is this really the fastest method?
If you want to decode complex objects or larger arrays, this is the fastest solution!
Besides being fast, this is the only solution that can reliably handle any kind of input value - other functions throw errors or return incorrect results in some cases.
If your JSON string contains short values (e.g., strings, numbers or objects with only 1-2 attributes) then all solutions in this SO questions come to a similar performance.
Here's a quick overview with comparison - you can find the test-cases in the linked gist. The last column uses the code from this answer:
PHP version: 7.4.21
test1: json_last_error() == JSON_ERROR_NONE
test2: is_object( json_decode() )
test3: json_decode() && $res != $string
test4: preg_match()
test5: "maybe decode" approach
| test1 | test2 | test3 | test4 | test5
#0 | 0.0147 | 0.0109 ✓︎ | 0.0119 | 0.0177 | 0.0194
#1 | 0.0129 | 0.0106 | 0.0098 | - INV - | 0.0078 ✓︎
#2 | 0.0076 | 0.0075 | 0.0063 ✓︎ | 0.0083 | 0.0133
#3 | 0.0126 | 0.0105 | 0.0096 ✓︎ | - INV - | 0.0172
#4 | 0.0070 | - INV - | 0.0061 ✓︎ | 0.0141 | 0.0134
#5 | 0.0114 | - INV - | 0.0101 | 0.0075 ✓︎ | 0.0168
#6 | 0.0203 | - INV - | 0.0195 | 0.0073 ✓︎ | 0.0259
#7 | 0.0046 | - INV - | - INV - | 0.0077 | 0.0031 ✓︎
#8 | 0.0066 | - INV - | - INV - | 0.0081 | 0.0020 ✓︎
#9 | 1.0781 | - INV - | 1.0555 | 0.0998 ✓︎ | 1.0385
#10 | 0.3183 ✓︎ | 0.3246 | 0.3270 | 1.0186 | 0.3311
#11 | 0.0071 | 0.0068 | 0.0067 ✓︎ | - INV - | 0.0079
#12 | - ERR - | - ERR - | - ERR - | - ERR - | 0.0025 ✓︎
#13 | - ERR - | - ERR - | - ERR - | - ERR - | 0.0024 ✓︎
Avg | 0.1251 | 0.0618 ✓︎ | 0.1463 | 0.1321 | 0.1072
Note that the fastest solution produces the most incorrect results. From all other solutions, the "maybe decode" approach is not only the fastest, but also the only solution with correct results.
Here is the full performance comparison script, there you can see the test-data I used for the comparison: https://gist.github.com/stracker-phil/6a80e6faedea8dab090b4bf6668ee461
The "maybe decode" logic/code
We first perform some type checks and string comparisons before attempting to decode the JSON string. This gives us the best performance because json_decode() can be slow.
/**
* Returns true, when the given parameter is a valid JSON string.
*/
function is_json( $value ) {
// Numeric strings are always valid JSON.
if ( is_numeric( $value ) ) { return true; }
// A non-string value can never be a JSON string.
if ( ! is_string( $value ) ) { return false; }
// Any non-numeric JSON string must be longer than 2 characters.
if ( strlen( $value ) < 2 ) { return false; }
// "null" is valid JSON string.
if ( 'null' === $value ) { return true; }
// "true" and "false" are valid JSON strings.
if ( 'true' === $value ) { return true; }
if ( 'false' === $value ) { return true; }
// Any other JSON string has to be wrapped in {}, [] or "".
if ( '{' != $value[0] && '[' != $value[0] && '"' != $value[0] ) { return false; }
// Verify that the trailing character matches the first character.
$last_char = $value[strlen($value) -1];
if ( '{' == $value[0] && '}' != $last_char ) { return false; }
if ( '[' == $value[0] && ']' != $last_char ) { return false; }
if ( '"' == $value[0] && '"' != $last_char ) { return false; }
// See if the string contents are valid JSON.
return null !== json_decode( $value );
}
Extra: Use this logic to safely double-decode JSON
This function uses the same logic but either returns the decoded JSON object or the original value.
I use this function in a parser that recursively decodes a complex object. Some attributes might be decoded already by an earlier iteration. That function recognizes this and does not attempt to double decode the value again.
/**
* Tests, if the given $value parameter is a JSON string.
* When it is a valid JSON value, the decoded value is returned.
* When the value is no JSON value (i.e. it was decoded already), then
* the original value is returned.
*/
function get_data( $value, $as_object = false ) {
if ( is_numeric( $value ) ) { return 0 + $value; }
if ( ! is_string( $value ) ) { return $value; }
if ( strlen( $value ) < 2 ) { return $value; }
if ( 'null' === $value ) { return null; }
if ( 'true' === $value ) { return true; }
if ( 'false' === $value ) { return false; }
if ( '{' != $value[0] && '[' != $value[0] && '"' != $value[0] ) { return $value; }
$json_data = json_decode( $value, $as_object );
if ( is_null( $json_data ) ) { return $value; }
return $json_data;
}
Note: When passing a non-string to any of the other solution in this SO question, you will get dramatically degraded performance + wrong return values (or even fatal errors). This code is bulletproof and highly performant.