Daniel Iuhas
Daniel Iuhas

Reputation: 33

Json from mysql breaks

i have some issue with a json stored in mysql TEXT field, the thing is that everything is OK until i use ' (single quote) the json then is saved \' and every time i save it back it appends a slash like: \' \\' \\' etc.

then some characters are not encoded correctly \u00e0

<div class="wpbs-calendar-legend" data-info="{&quot;default&quot;:{&quot;name&quot;:{&quot;default&quot;:&quot;Available&quot;,&quot;hr&quot;:&quot;Slobodno&quot;,&quot;cs&quot;:&quot;Volno&quot;,&quot;da&quot;:&quot;Ledigt&quot;,&quot;nl&quot;:&quot;Vrij&quot;,&quot;en&quot;:&quot;Available&quot;,&quot;fr&quot;:&quot;Libre&quot;,&quot;de&quot;:&quot;Frei&quot;,&quot;hu&quot;:&quot;Szabad&quot;,&quot;it&quot;:&quot;Libero&quot;,&quot;ro&quot;:&quot;Disponobil&quot;,&quot;ru&quot;:&quot;\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u043e&quot;,&quot;sk&quot;:&quot;Vo\u013en\u00fd&quot;,&quot;es&quot;:&quot;Libre&quot;,&quot;sv&quot;:&quot;Ledigt&quot;,&quot;uk&quot;:&quot;B\u0456\u043b\u044c\u043d\u043e&quot;,&quot;no&quot;:&quot;&quot;},&quot;color&quot;:&quot;#f1ffcc&quot;,&quot;splitColor&quot;:false,&quot;bookable&quot;:&quot;yes&quot;,&quot;auto-pending&quot;:&quot;no&quot;,&quot;sync&quot;:&quot;no&quot;},&quot;1&quot;:{&quot;name&quot;:{&quot;default&quot;:&quot;Booked&quot;,&quot;hr&quot;:&quot;Zauzeto&quot;,&quot;cs&quot;:&quot;Obsazeno&quot;,&quot;da&quot;:&quot;Booket&quot;,&quot;nl&quot;:&quot;Bezet&quot;,&quot;en&quot;:&quot;Booked&quot;,&quot;fr&quot;:&quot;Occup\u00e9&quot;,&quot;de&quot;:&quot;Belegt&quot;,&quot;hu&quot;:&quot;Foglalt&quot;,&quot;it&quot;:&quot;Prenotato&quot;,&quot;ro&quot;:&quot;Rezervat&quot;,&quot;ru&quot;:&quot;\u0417\u0430\u043d\u044f\u0442\u043e&quot;,&quot;sk&quot;:&quot;Obsaden\u00fd&quot;,&quot;es&quot;:&quot;Reservado&quot;,&quot;sv&quot;:&quot;Bokat&quot;,&quot;uk&quot;:&quot;\u0417\u0430\u0439\u043d\u044f\u0442\u043e&quot;,&quot;no&quot;:&quot;&quot;},&quot;color&quot;:&quot;#ff524c&quot;,&quot;splitColor&quot;:false,&quot;bookable&quot;:false,&quot;auto-pending&quot;:&quot;yes&quot;,&quot;sync&quot;:&quot;yes&quot;},&quot;2&quot;:{&quot;name&quot;:{&quot;en&quot;:&quot;>14H&quot;,&quot;nl&quot;:&quot;&quot;,&quot;et&quot;:&quot;&quot;,&quot;fi&quot;:&quot;&quot;,&quot;fr&quot;:&quot;D\u00e8s 14H&quot;,&quot;de&quot;:&quot;&quot;,&quot;pt&quot;:&quot;&quot;,&quot;ro&quot;:&quot;&quot;,&quot;default&quot;:&quot;D\u00e8s 14H&quot;},&quot;color&quot;:&quot;#f1ffcc&quot;,&quot;splitColor&quot;:&quot;#ff524c&quot;,&quot;bookable&quot;:&quot;yes&quot;,&quot;hide&quot;:false,&quot;auto-pending&quot;:&quot;no&quot;,&quot;sync&quot;:&quot;no&quot;},&quot;3&quot;:{&quot;name&quot;:{&quot;en&quot;:&quot;<12H&quot;,&quot;nl&quot;:&quot;&quot;,&quot;et&quot;:&quot;&quot;,&quot;fi&quot;:&quot;&quot;,&quot;fr&quot;:&quot;Jusqu\\" \u00e0="" 10h","de":"","pt":"","ro":"","default":"jusqu\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\u00e0="" 10h"},"color":"#ff524c","splitcolor":"#f1ffcc","bookable":"yes","hide":false,"auto-pending":"no","sync":"no"}}'=""></div>

and in my insepct window looks like this:

developer console

I am using WordPress so this json_encoded string is saved using update_option into the database. I've tried to stripslashes() the json encoded string on update_option but that did not solve the issue.

may somebody can help me out here?

Thanks for your time on this!

Upvotes: 1

Views: 628

Answers (2)

Rick James
Rick James

Reputation: 142298

No, not htmlentities, not stripslashes. If you got the JSON from json_encode(), use an extra parameter to avoid the "Unicode" encoding:

$x = json_encode($s, JSON_UNESCAPED_UNICODE);

See Example #2 in http://php.net/manual/en/function.json-encode.php (Requires PHP 5.4.0)

Upvotes: 1

Simos Fasouliotis
Simos Fasouliotis

Reputation: 1390

It looks like your SQL insert is escaping - very correctly - the data.

Now when you need to get the data from SQL you need to remove escaping by the use of stripslashes.

So if your variable is $jsonResult use it as stripslashes($jsonResult).

Upvotes: 1

Related Questions