What potential pitfalls should be considered when using json_decode in PHP?

When using json_decode in PHP, it's important to be aware of potential security risks such as code injection attacks. To prevent this, always validate and sanitize the JSON data before decoding it. Additionally, make sure to handle any errors that may occur during the decoding process to prevent unexpected behavior in your application.

$json_data = '{"key": "value"}';
$decoded_data = json_decode($json_data);

if ($decoded_data === null && json_last_error() !== JSON_ERROR_NONE) {
    // Handle error, such as invalid JSON format
} else {
    // Process the decoded data
}