1.2.412. Empty Json Error

json_last_error() keeps the last error that was generated while decoding a JSON string. To reset this cache to empty, one must run a call to json_decode() that succeed. This leads some code to make an apparently pointless call, just to empty the error cache, and avoid confusing the message with the one of a previous call.

<?php

// This generates an error
$json = json_decode([);

$json = json_decode($valid_json);

echo json_last_error(); // This error is confused for the last call, not the first one.

// pointless call, except to empty the cache.
$json = json_decode([]);

$json = json_decode($valid_json);

echo json_last_error(); // This error is dedicated to the last call

?>

1.2.412.1. Specs

Short name

Structures/EmptyJsonError

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.6.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud