Jhim Jhim - 2 months ago 4x
JSON Question

How to deal with Double Quoted String inside a Json object?

So I have a json encoded string by a system, Which for a reason I cannot touch.
See below.

[{"item0":"sometext","item1":"sometext too but i have "quoted string" inside of me"}]

so now my problem is, using
gives me NULL output as it cannot convert the quoted string...

I try some preg_replace code but am too noob to findout how to replace the quoted string and introduce escape char which will look like this
\"quoted string\"
. Seriously, I cannot comprehend with the preg_replace with this condition.. where you will find the occurence of double qoute inside the json_encoded string.. Please enlighten me.

I have tried other questions available here but my understanding was not enough.
Also, pls note that I cannot touch the 1 encoding the json object as it is provided by a 3rd party system..


Thanks to those who enlighten me...
So this was not possible and I have to drop this one and try to contact the system developer to correct the json encoded string they provided as it was the best option.


Which for a reason I cannot touch

Then you're stuffed. It's broken, and you can't reliably fix it.

I try some preg_replace code

You can't. There's no way for you to know whether a " is actually meant to terminate the string, or meant to be a character in the string.

Stop all attempts to fix this at your end. The end sending you the invalid JSON is the problem. If you "can't touch" it, contact and berate someone who can until they fix it. You might take it as an opportunity to teach them that this is why don't hand-create JSON, or create it with string concatenation, etc. Instead, you build a structure, then use a proper JSON serializer to create the JSON, which will (in this case) put in the necessary escapes (backslashes) so that it looks like this:

[{"item0":"sometext","item1":"sometext too but i have \"quoted string\" inside of me"}]