What are some best practices for handling JSON data before inserting it into a database in PHP?

When handling JSON data before inserting it into a database in PHP, it is important to properly sanitize and validate the data to prevent SQL injection attacks and ensure data integrity. One way to achieve this is by using PHP's built-in functions like json_decode() to parse the JSON data and filter_var() to sanitize input values.

// Sample JSON data
$jsonData = '{"name": "John Doe", "age": 25}';

// Decode JSON data
$data = json_decode($jsonData, true);

// Sanitize input values
$name = filter_var($data['name'], FILTER_SANITIZE_STRING);
$age = filter_var($data['age'], FILTER_VALIDATE_INT);

// Insert sanitized data into the database
// $db->query("INSERT INTO users (name, age) VALUES ('$name', $age)");