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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With