0

I have a problem with encoding and decoding json data. In js I send query with data type 'json', it looks like this:

{\"front\":{\"0\":{\"type\":\"text\",\"width\":\"55px\",\"height\":\"27px\",\"top\":\"151px\",\"left\":\"86px\",\"zIndex\":\"1\",\"svg\":\"<svg width=\\\"54.9375\\\" height=\\\"27.09375\\\" viewBox=\\\"0 0 54.9375 27.09375\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\"><g id=\\\"0.7882792934370437\\\"><text fill=\\\"#FF0000\\\" stroke=\\\"none\\\" stroke-width=\\\"0\\\" stroke-linecap=\\\"round\\\" stroke-linejoin=\\\"round\\\" x=\\\"\\\" y=\\\"\\\" text-anchor=\\\"start\\\" font-size=\\\"24px\\\" font-family=\\\"arial\\\" data-textcurve=\\\"1\\\" data-itemzoom=\\\"1 1\\\" data-textspacing=\\\"0\\\"><textPath xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\" xlink:href=\\\"http://138.68.62.219/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\"><tspan dy=\\\"0\\\">Hello</tspan></textPath></text></g><defs><path id=\\\"textPath-item-0\\\" d=\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\"></path></defs></svg>\",\"rotate\":0,\"text\":\"Hello\",\"color\":\"#FF0000\",\"fontFamily\":\"arial\",\"align\":\"center\",\"outlineC\":\"none\",\"outlineW\":0}}

Then I save it to cell in MySQL, it looks like this:

{\\&quot;front\\&quot;:{\\&quot;0\\&quot;:{\\&quot;type\\&quot;:\\&quot;text\\&quot;,\\&quot;width\\&quot;:\\&quot;55px\\&quot;,\\&quot;height\\&quot;:\\&quot;27px\\&quot;,\\&quot;top\\&quot;:\\&quot;151px\\&quot;,\\&quot;left\\&quot;:\\&quot;86px\\&quot;,\\&quot;zIndex\\&quot;:\\&quot;1\\&quot;,\\&quot;svg\\&quot;:\\&quot;&lt;svg width=\\\\\\&quot;54.9375\\\\\\&quot; height=\\\\\\&quot;27.09375\\\\\\&quot; viewBox=\\\\\\&quot;0 0 54.9375 27.09375\\\\\\&quot; xmlns=\\\\\\&quot;http:\/\/www.w3.org\/2000\/svg\\\\\\&quot; xmlns:xlink=\\\\\\&quot;http:\/\/www.w3.org\/1999\/xlink\\\\\\&quot;&gt;&lt;g id=\\\\\\&quot;0.7882792934370437\\\\\\&quot;&gt;&lt;text fill=\\\\\\&quot;#FF0000\\\\\\&quot; stroke=\\\\\\&quot;none\\\\\\&quot; stroke-width=\\\\\\&quot;0\\\\\\&quot; stroke-linecap=\\\\\\&quot;round\\\\\\&quot; stroke-linejoin=\\\\\\&quot;round\\\\\\&quot; x=\\\\\\&quot;\\\\\\&quot; y=\\\\\\&quot;\\\\\\&quot; text-anchor=\\\\\\&quot;start\\\\\\&quot; font-size=\\\\\\&quot;24px\\\\\\&quot; font-family=\\\\\\&quot;arial\\\\\\&quot; data-textcurve=\\\\\\&quot;1\\\\\\&quot; data-itemzoom=\\\\\\&quot;1 1\\\\\\&quot; data-textspacing=\\\\\\&quot;0\\\\\\&quot;&gt;&lt;textPath xmlns:xlink=\\\\\\&quot;http:\/\/www.w3.org\/1999\/xlink\\\\\\&quot; xlink:href=\\\\\\&quot;http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\&quot;&gt;&lt;tspan dy=\\\\\\&quot;0\\\\\\&quot;&gt;Hello&lt;\/tspan&gt;&lt;\/textPath&gt;&lt;\/text&gt;&lt;\/g&gt;&lt;defs&gt;&lt;path id=\\\\\\&quot;textPath-item-0\\\\\\&quot; d=\\\\\\&quot;M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\&quot;&gt;&lt;\/path&gt;&lt;\/defs&gt;&lt;\/svg&gt;\\&quot;,\\&quot;rotate\\&quot;:0,\\&quot;text\\&quot;:\\&quot;Hello\\&quot;,\\&quot;color\\&quot;:\\&quot;#FF0000\\&quot;,\\&quot;fontFamily\\&quot;:\\&quot;arial\\&quot;,\\&quot;align\\&quot;:\\&quot;center\\&quot;,\\&quot;outlineC\\&quot;:\\&quot;none\\&quot;,\\&quot;outlineW\\&quot;:0}}

When I read with php this data that's what I see ( $tmp=$products[0]['design_file']; print_r($tmp);) :

"{\\"front\\":{\\"0\\":{\\"type\\":\\"text\\",\\"width\\":\\"55px\\",\\"height\\":\\"27px\\",\\"top\\":\\"151px\\",\\"left\\":\\"86px\\",\\"zIndex\\":\\"1\\",\\"svg\\":\\"<svg width=\\\\\\"54.9375\\\\\\" height=\\\\\\"27.09375\\\\\\" viewBox=\\\\\\"0 0 54.9375 27.09375\\\\\\" xmlns=\\\\\\"http:\/\/www.w3.org\/2000\/svg\\\\\\" xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\"><g id=\\\\\\"0.7882792934370437\\\\\\"><text fill=\\\\\\"#FF0000\\\\\\" stroke=\\\\\\"none\\\\\\" stroke-width=\\\\\\"0\\\\\\" stroke-linecap=\\\\\\"round\\\\\\" stroke-linejoin=\\\\\\"round\\\\\\" x=\\\\\\"\\\\\\" y=\\\\\\"\\\\\\" text-anchor=\\\\\\"start\\\\\\" font-size=\\\\\\"24px\\\\\\" font-family=\\\\\\"arial\\\\\\" data-textcurve=\\\\\\"1\\\\\\" data-itemzoom=\\\\\\"1 1\\\\\\" data-textspacing=\\\\\\"0\\\\\\"><textPath xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\" xlink:href=\\\\\\"http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\"><tspan dy=\\\\\\"0\\\\\\">Hello<\/tspan><\/textPath><\/text><\/g><defs><path id=\\\\\\"textPath-item-0\\\\\\" d=\\\\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\"><\/path><\/defs><\/svg>\\",\\"rotate\\":0,\\"text\\":\\"Hello\\",\\"color\\":\\"#FF0000\\",\\"fontFamily\\":\\"arial\\",\\"align\\":\\"center\\",\\"outlineC\\":\\"none\\",\\"outlineW\\":0}}

If I do this:

$tmp=$products[0]['design_file'];
$info=json_decode($tmp);
print_r($info);

It gives me Null. How can I get my array back from that mess?

Javascript sending:

jQuery(document).triggerHandler( "before.addtocart.design", datas);
var finaldata=JSON.stringify(datas);
    jQuery.ajax({
    url: 'index.php?route=checkout/cart/add', //URL TO CONTROL FUNCTION add()
    type: 'post',
    data: 'product_id=' + product_id + '&quantity=' + q+"&option['options']="+finaldata,

Read in Php

if (isset($this->request->post['option'])) {
            $option = array_filter($this->request->post['option']);

Pass to view

$data['products'][] = array(
                'design_file'=>$product['design_file'],
...
$tmp=$products[0]['design_file'];
tttaaabbb
  • 407
  • 7
  • 19
  • Your issue is that you do _not_ save the content you actually want inside MySQL, but something escaped. So the question is: _why_? We need your code for that... _do not try to fix the symptom, fix the cause!_ – arkascha Sep 16 '16 at 19:03
  • How are you sending this data in the first place? – gen_Eric Sep 16 '16 at 19:15
  • @arkascha @rocket-hazmat .ok thereis js `var finaldata=JSON.stringify(datas); data: 'product_id=' + product_id + '&quantity=' + q+"&option['options']="+finaldata, //in ajax body` there is save to mysql `if (isset($this->request->post['option'])) { $option = array_filter($this->request->post['option']);//further standart opencart code` thats how i read: `$data['products'][] = array( 'design_file'=>$product['design_file'] ` in tpl file `$tmp=$products[0]['design_file'];` – tttaaabbb Sep 16 '16 at 19:17
  • Sorry, but you do not expect anyone to be able to read that, do you? Add all relevant information to the question itself please. There is an `edit` button below your question. _Use it._ – arkascha Sep 16 '16 at 19:40
  • You are probably on a PHP version that still has magic_quotes, which means you're on a highly outdated/obsolete php. – Marc B Sep 16 '16 at 20:10
  • @MarcB yeah, you are right, i try to use `stripslashes` it realy cuts ` \\ ` but i still cant `json_decode` – tttaaabbb Sep 16 '16 at 20:27
  • better solution: upgrade php. magic_quotes was/is utter crap and should never have seen the light of day. any version of php which still has it enabled by default is by definition obsolete and has other problems. get out of the php stoneage, basically... – Marc B Sep 16 '16 at 20:29

2 Answers2

2

You might be storing malformed JSON data. Take a look at these pages (some useful resources right from PHP docs):

PS.: The second link is related to the options parameter used in json_encode. You might compose those values the way you want, just like the example below:

$obj = [
  "prop1" => 123,
  "prop2" => [
    "prop2.1" => "Value A",
    "prop2.2" => [1, 2, 3, 4, 5]
  ]
];

$jsonContent = json_encode($obj, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);

By the way, you still can send your JavaScript objects to the server using a contentType: 'application/json' setting in $.ajax function call. That will enable you to handle request data from that HTTP request as a PHP object, as it really should be.

Use PHP to serialize your JSON content in order to store it in database instead of bypassing the string directly from the request attributes.

mstred
  • 36
  • 4
  • i did as you said, it worked, but just for first lvl, ` \" ` still here, and i can't decode whole object `$tmp=$products[0]['design_file']; $info=json_decode($tmp,true );//WORKS $info=json_decode(stripslashes($info['options']));//DOESNT` – tttaaabbb Sep 16 '16 at 20:32
  • You were right troubles in the wrapping data in json. If you interested `var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':datas// I HAD JSON.stringify(datas) }` – tttaaabbb Sep 16 '16 at 21:12
0

Something is really messed up here. First, your initial JSON is not valid. Somehow it is already has extra slashes.

Check here: Issue with JSON.stringify adding a extra \ and "" to my Json object. Maybe here: JSON.stringify escaping without need

Also there is missing } in the very end of JSON string (it should be }}} three closing brackets).

Secondly, I guess before you inserted it in database you escaped it (so thats how you got even more slashes) and you did htmlspecialchars(). Considering this, thats how you can parse it in object.

$info = json_decode(stripslashes(htmlspecialchars_decode($tmp.'}')));
Community
  • 1
  • 1
Alexey Chuhrov
  • 1,787
  • 12
  • 25
  • missed '{' probably my fault , i've reworked the way i wrap data in js, `var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':JSON.stringify(datas) } } data: finaldata,` right after i was able to do this `
            
            
    ` result:
    – tttaaabbb Sep 16 '16 at 20:11
  • You should also `htmlspecialchars_decode()` since double quotes encoded in `"`. But thats not the real solution... – Alexey Chuhrov Sep 16 '16 at 20:39
  • After i decoded root json, there is few left, i managed to fo this: `$info=json_decode($tmp,true ); print_r(($info['options']));` result `{"product_id":"176","colors":{"0":"000000"},"print":{"sizes":"{\"front\":{\"width\"...` i cant decode this string now in youw way even with `htmlspecialchars_decode` – tttaaabbb Sep 16 '16 at 20:41
  • Well, what I actually did is just copied the JSON string from your answer `{\\"front\\":{\\"0\\&...` (this one) put it in variable `$tmp` and then wrote this string `$info = json_decode(stripslashes(htmlspecialchars_decode($tmp.'}')))‌​;`, which gave me valid object. And right now, Im sorry I have no idea whats going on in your code. Maybe you should add more code samples in the question. Have you considered to turn off `magic_quotes` or upgrade PHP ? – Alexey Chuhrov Sep 16 '16 at 21:01
  • I've just found error in JS, it was rly silly, my fault... If you interested `var finaldata={ 'product_id': product_id, 'quantity':q, 'option':{ 'options':datas// I HAD JSON.stringify(datas) } }` – tttaaabbb Sep 16 '16 at 21:11