0

I need to get a JSON Object from Database, convert it to php object and foreach the key=>value pairs.

The JSON Object is stored with JSON.stringify(obj) in MySQL text field. Afterwards i get the data with PDO, but i have same unterstanding problem how to decode the json object to an object in php. The Result is everytime a string.

$export = $data['document']->tax_rates;
$export = json_encode($export);
$export = json_decode($export);
var_dump($export);
echo($export)

// only test 
var_dump(json_decode($data['document']->tax_rates));
var_dump($data['document']->tax_rates)

$jsontest = json_decode('{"a":1,"b":2}');
var_dump($jsontest);

The result is:

string(42) "{"7":39729.69,"19":107.73}" // var_dump
{"7":39729.69,"19":107.73}  // echo

NULL // result of decode without encode before
string(42) "{"7":39729.69,"19":107.73}"  // var_dump raw data['document']

//test result as it should be
object(stdClass)#21 (2) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
}

Debugging the Json output the error: JSON_ERROR_SYNTAX. But is is a valid JSON Object.

How can i genereate an php object here and where is my issue in this case? If i understand correct, the output is a string because since it is stored in the text field, it is already a string. If it is so, is it possible to handle it this way or is it impossible? Thanks.

Marco
  • 131
  • 1
  • 9
  • 1
    Well, you only need to encode (and decode) once. Data is already encoded, yet the first thing you do when you retrieve it is to encode it again. – Álvaro González Jun 20 '20 at 13:02
  • If i only encode the result of var_dump ist still string: string(44) ""{"7":39729.69,"19":107.73}"" – Marco Jun 20 '20 at 13:06
  • If i remove the line $export = json_encode($export); so the result is NULL – Marco Jun 20 '20 at 13:09
  • I removed my comment because, honestly, I don't know what you're trying to illustrate with your test case so I can't give much advice. `{"7":39729.69,"19":107.73}` can't possibly have 42 bytes, not even in UTF-16, so it can't be the actual raw data. – Álvaro González Jun 20 '20 at 13:15
  • The mysql field is utf8_general_ci and the pdo also fetch in UTF-8. The value in Database looks like {"7":39729.69,"19":107.73} – Marco Jun 20 '20 at 13:21
  • 1
    There you are: that isn't valid JSON. Trying pasting `[{"7":3.5,"19":19,"20":20}]` that in [jsonlint.com/](https://jsonlint.com/) – Álvaro González Jun 20 '20 at 13:22
  • Okay, so my problem is storing the json object with JSON.stringify(obj) in mysql. I already searched a solution why the quation marks stored as unicode but found no solution yet. Thank you, i have to check it again! – Marco Jun 20 '20 at 13:25
  • Are you trying to implement a home-brew solution for SQL injection? – Álvaro González Jun 20 '20 at 13:26
  • I only try to save the the json to database .... this works now, thanks! $html = $data['document']->tax_rates; $json = html_entity_decode($html); $json = json_decode($json); var_dump($json); – Marco Jun 20 '20 at 13:31
  • Just use prepared statements as explained at [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). SQL doesn't have anything to do with HTML! – Álvaro González Jun 20 '20 at 13:34
  • I use prepared statements already :) – Marco Jun 20 '20 at 13:36
  • Well, there've never been any builtin feature in PHP to encode input as HTML automagically so you must be doing it yourself, somewhere :) – Álvaro González Jun 20 '20 at 13:37

1 Answers1

2

What you have is not JSON but HTML-encoded JSON. You can recover the original data with html_entity_decode():

$html = '[{"7":3.5,"19":19,"20":20}]';
$json = html_entity_decode($html);
echo $json;

But it'd be better to identify and fix the bug that's causing invalid data form being stored in the first place.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360