Is encoded json damaged / escaped by PostgresQL?

I am using the Yii2 model, which is connected to the PostgresQL database. I have a behavior that encodes and decodes some attributes of this model to / from json. For encoding / decoding, I use the Json helper, the Json::encode and Json::decode .

The table column is of type json. An example of what gets into the database:

 "{\"additional_tags\":[\"#\",\"#\"],\"vk\":\"vk.com\\\/privetik\"}" 

When I try to decode it back to a php array, here is what came back instead:

 '{"additional_tags":["#","#"],"vk":"vk.com\/privetik"}' 

EDIT: think about it, the line seems fine, but the behavior of the ::decode method is weird. Essentially, all he does is delete the slash copies, not convert it to a php array or throw an exception.

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

+5
source share
1 answer

It looks like the string in your database is double-encoded. Try passing it through Json::decode another time, I'm sure it will return your array.

+1
source

Source: https://habr.com/ru/post/1234277/


All Articles