abirz
abirz

Reputation: 21

How to unescape double quotes in Symfony using JMS Serializer

I'm using JMS Serializer (jms/serializer-bundle) in Symfony 3.4 to serialize an object to JSON.

The object contains a property called text which is a string value containing double quotes. JMS Serializer escapes all the double quotes with a backslash on serialization. So the field looks like this on output:

"text": "<p>\"My concern is the President. It's not okay, it's horrible,\" he said.</p>"

This is okay for normal strings, but if the string contains json itself, it becomes invalid json

"text": "[[{\"fid\":\"123456\",\"view_mode\":\"content\",\"attributes\":{\"height\":\"400\",\"width\":\"800\"}}]]"

How can I get it to not escape double quotes on JSON Serialization?

Upvotes: 1

Views: 1001

Answers (1)

Sondre Edvardsen
Sondre Edvardsen

Reputation: 180

The value of the "text" property is still a string, or a string of serialized json. This is why it escapes the quotes.

One solution would be to deserialize the json string, so that the value of "text" is a nested object, before you serialize the whole object.

Alternatively you could try stripcslashes on the string after deserialization.

Upvotes: 0

Related Questions