17

I'm using this simple code to transform database query results into JSON format:

$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);

echo stripslashes(json_encode($data));

The problem is that if there are double quotes in the data (e.g. in the product column) returned by this query. The json_encode function does not encode the data in a good JSON format.

Could someone help me how to escape the double quotes that are returned by the query? Thank you.

3
  • Can you give an example of what you mean by double quotes? Commented Jan 13, 2016 at 14:53
  • Escape them before inserting them in the database? Or using ansi? Commented Jan 13, 2016 at 14:53
  • 1
    FWIW Re "The json_encode function does not encode the data in a good JSON format." Actually, it does. Problems arise when you want to do anything other than parse incoming json directly into a JS object or array, using JSON.parse. You can prove it works for yourself by reading any json file, created by json_encode, from JS. Yes, there are documented situations where you don't get exactly what you expect (hence various json_encode options). But what you won't get, is a parse failure. NOW, once you start echoing into the HTML text being sent to browser, you have a different situation... Commented Oct 28, 2019 at 18:18

6 Answers 6

32

You will need htmlspecialchars instead of stripslashes with proper encoding (UTF-8, if your page uses UTF-8 charset) and ENT_QUOTES which will escape double quotes preventing data to break. See the code below:

echo htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8');
Sign up to request clarification or add additional context in comments.

Comments

8

json_encode already takes care of this, you are breaking the result by calling stripslashes:

echo json_encode($data); //properly formed json

2 Comments

@tronman do you have an actual repeatable example of json_encode not escaping double quotes that you can share. Perhaps I missed some edge cases 8 years ago. Happy to amend if so
@tronman ak ok, thanks for responding, and glad you solved your issue
4

Example with a simple array with double quotes string value.

$yourArr = array(
    'title' => 'This is an example with "double quote" check it'
);

// add htmlspecialchars as UTF-8 after encoded
$encodeData = htmlspecialchars(json_encode($yourArr), ENT_QUOTES, 'UTF-8');

echo $encodeData;

Result:

{"title":"This is an example with \"double quote\" check it"}

According to PHP Manual:

That said, quotes " will produce invalid JSON, but this is only an issue if you're using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

2 Comments

share manual just for indicate either json valid or invalid... there are so many options aviallable in manual for escaping quotes.. @Steve
Yes, sure - but you quote a paragraph that simply isnt there. Maybe it did in 2011...
-1

to encode the quotes using htmlspecialchars:

$json_array = array(
'title' => 'Example string\'s with "special" characters'
);

$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');

Comments

-1

Yes, PHP json_encode wouldn't "escape double" quotes, we need to escape those manually in this super simple way-

array_walk($assoc_array, function(&$v, $k) {
  if(is_string($v) && strpos($v, '"') !== false) {
    $v = str_replace('"', '\"', $v);
  }
});
// After escaping those '"', you can simply use json_enocde then.
$json_data = json_encode($assoc_array);

Comments

-2

You can done that using base64_encode and base64_decode

To store in database first convert it to base64_encode and stored into database and if you want that data then you can decrypt that data using base64_decode

$var['cont'] = base64_encode($_POST['data']);
$dd =  json_encode($var['cont']);
echo base64_decode ( json_decode($dd) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.