Y
Y
Yasuro2018-03-15 23:49:52
PHP
Yasuro, 2018-03-15 23:49:52

Why does mysql escape quotes in json?

Created a table, made the field type json.
I wrote an array from php into this field, after converting it to json. json nested. Everything was recorded as it should.
Now I add similar entries to this field using JSON_ARRAY_APPEND (), and as a result, this entry is added: "{/"prop1/":/"value1/"}" . The whole essence of json in mysql is lost.
PHP code:

$array=array(
"prop1"=>"value1",
"prop2"=>"value2"
);
$jsArr=json_enecode($array);
$sql="update user from friend set=JSON_ARRAY_APPEND(user,'$','".$jsArr."')";

What am I doing wrong?
Ps I have been working with php and sql not long ago, I wrote from memory, maybe I made a mistake in the syntax, but I think the essence is clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-03-16
@Yasuro

Because you are not passing JSON, but a string. If you want it to be treated exactly as JSON, specify it explicitly via CAST('$jsArr' AS JSON).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question