How can the error message obtained from json_last_error_msg() be helpful in troubleshooting JSON decoding issues in PHP?

When decoding JSON in PHP, sometimes issues may arise due to incorrect formatting or syntax errors in the JSON data. In such cases, the error message obtained from json_last_error_msg() can be helpful in identifying the specific problem that caused the decoding to fail. By analyzing this error message, developers can pinpoint the issue and make necessary adjustments to resolve it.

$jsonData = '{"key": "value"}'; // JSON data with potential decoding issues
$decodedData = json_decode($jsonData);

if ($decodedData === null && json_last_error() !== JSON_ERROR_NONE) {
    $errorMessage = json_last_error_msg();
    echo "JSON decoding error: " . $errorMessage;
    // Troubleshoot and fix the JSON data to resolve decoding issues
}