PHP json_encode data with double quotes
You are running into a classic misunderstanding of how json_encode operates versus what stripslashes actually does. In your current implementation, you are fetching data from the database and attempting to sanitize it for JSON output by stripping slashes. This approach fails because the underlying issue isn't an extra layer of escaping; it is that json_encode has not yet processed the string content.
When PHP's json_encode function encounters a string containing double quotes (e.g., "Product A"), it does not leave them as bare characters in the output. Instead, it automatically escapes them by prefixing them with a backslash (\"). This is standard JSON behavior. However, if your database column stores data with single quotes surrounding it or if you have previously applied addslashes (which often happens when using prepared statements that aren't strictly necessary for simple SELECTs), you might end up with double slashes like \\\".
The line echo stripslashes(json_encode($data)); is the culprit here. You are calling json_encode, which correctly outputs valid JSON with escaped quotes, and then immediately stripping those backslashes. This turns valid JSON sequences into invalid characters for the parser. If your database returns a value like "Apple", json_encode creates \"Apple\". Your stripslashes call removes the backslash, resulting in "Apple". While this looks correct visually, if the data actually contained an escaped quote inside the string (like It said "Hello"), the logic breaks. More critically, relying on stripslashes assumes a specific prior state of the data that is hard to maintain and debug.
To fix this, you should stop using stripslashes entirely and trust json_encode to handle the escaping. The function is designed to escape double quotes, backslashes, newlines, and other special characters automatically based on the encoding options you provide.
Here is the corrected version of your code snippet:
$result = $mysqli->query("
SELECT
date as a
, sum(sales) as b
, product as c
FROM
default_dataset
GROUP BY
date
, product
ORDER BY
date
");
$data = $result->fetch_all(MYSQLI_ASSOC);
// Remove stripslashes entirely. Let json_encode handle escaping.
echo json_encode($data, JSON_UNESCAPED_UNICODE);
By removing stripslashes, you ensure that any double quotes present in the product column are correctly prefixed with a backslash in the final output string. The resulting text is valid JSON that can be safely parsed by JavaScript or any other client-side logic without throwing syntax errors.
There is a nuance regarding Unicode characters as well. If your dataset contains non-ASCII characters (like emojis or accented letters), json_encode defaults to escaping them as \uXXXX. While this is technically valid, it often renders poorly in modern browsers if not handled correctly by the client. You might prefer to use the JSON_UNESCAPED_UNICODE flag, as shown above, which outputs UTF-8 characters directly. However, be aware that this increases payload size slightly and can introduce encoding issues if the receiving end isn't strictly UTF-8 compliant.
Another consideration is how you handle null values or floating-point numbers in the aggregated data. If your sum(sales) returns a float with many decimal places, json_encode will output them as-is. Depending on your backend requirements, you might need to round these values before encoding or configure MySQL to store sums as integers if precision isn't critical.
Finally, consider switching from raw SQL queries to prepared statements if you ever start inserting data that contains quotes. While SELECT statements are generally safe from injection, maintaining consistent data integrity across your schema is vital. If you move toward an ORM like Eloquent in a Laravel project, the framework handles these JSON serialization concerns internally when returning API responses, often abstracting away the need to manually call json_encode. For more complex scenarios involving large datasets or specific formatting requirements, checking resources from LaravelCompany can provide additional patterns on structuring API responses effectively.