Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoded json gets corrupted/escaped by PostgresQL?

I am using a Yii2 model that is hooked to a PostgresQL database. I have a behavior that encodes and decodes certain attributes of this model to/from json. To encode/decode, I am using the Json helper, the Json::encode and Json::decode methods.

The column in the table is of a json type. An example of what ends up in the database:

"{\"additional_tags\":[\"#здрасте\",\"#кафе\"],\"vk\":\"vk.com\\\/privetik\"}"

When I try to decode it back into a php array, here's what's returned instead:

'{"additional_tags":["#здрасте","#кафе"],"vk":"vk.com\/privetik"}'

EDIT: Come to think of it, the string seems fine, but the behavior of the ::decode method is strange. Essentially, all it does is remove the escape slashes, instead of converting it into a php array or throwing an exception.

What should I do to fix this? Appreciate any feedback.

like image 484
nainy Avatar asked Nov 10 '22 03:11

nainy


1 Answers

Looks like the string in your database is encoded twice. Try passing it through Json::decode another time, I bet it will return your array.

like image 146
Beowulfenator Avatar answered Nov 14 '22 22:11

Beowulfenator