-1

I have this well formed JSON oject (verified on website json verificator):

{"isNew":true,"creationDate":"2022/06/04","lastUpdate":"2022/06/04","IdStatus":2,"optionsObject":[{"id":"1","name":"XXXXXX","description":"XXXXXXXXXXXX","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"},{"id":"6","name":"YYYYYY","description":"YYYYYYYYY","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"},{"id":"7","name":"ZZZZZZZ","description":"ZZZZZZZZZZ","qty":"0","price":"0.00","label":"","placeholder":"","enabled":"1"}],"Id":"178","items":"4","startDate":"20/06/2022"}

and obtaining this object in PHP with this code:

$json_object = str_replace("\\", "", $data);
$singleObject = json_decode( $json_object );

$sql = "INSERT INTO table1(field1, field2, field3, field4, field5, field6, field7) VALUE ("  .$singleObject->Id . ", '" . $singleObject->startDate ."', " . $singleObject->IdStatus . ", " . $singleObject->IdRoom . ", " . $singleObject->creationDate . ", " . $singleObject->lastUpdate . ", " . $singleObject->items . "')";

file_put_contents(plugin_dir_path( __FILE__ ) . '/testfile2.log', print_r( $sql . PHP_EOL, true), FILE_APPEND);

I can see in testfile2.log I have well formed sql sentence, but if I add also this code:

$optionsobject = json_decode($json_object, true);

foreach ($optionsobject->optionsObject as $optionobj) {
    $sql_options = "INSERT INTO table2 (field1, field2, field3, field4, field5) VALUES(" . $id . ", " . $optionobj->id . ", " 
                    $optionobj->qty .", " . $optionobj->price . ", '" . $optionobj->placeholder . "')";
                    
    file_put_contents(plugin_dir_path( __FILE__ ) . '/testfile.log', print_r( $sql_options . PHP_EOL, true), FILE_APPEND);
}

I obtain this error :

[04-Jun-2022 09:48:33 UTC] PHP Parse error:  syntax error, unexpected '$optionobject' (T_VARIABLE) in...

when I know usually to loop object it's used this foreach as I can see in several examples... is there maybe a detail I missed with this foreach?

Thanks in advance to all! Cheers! :-)

Luigino
  • 745
  • 3
  • 7
  • 26
  • The shown code does not fit to the message, there is no `$optionobject`, only `$optionsobject` and `$optionobj` (and some more, but not resembling) – Honk der Hase Jun 04 '22 at 08:57
  • The error message has nothing to do with the JSON, it's saying that *your PHP code* has a typo in it. Look very closely at the line it indicates. – IMSoP Jun 04 '22 at 08:59

1 Answers1

1

you have a missing dot . before $optionobj->qt

Omar Tammam
  • 1,217
  • 1
  • 9
  • 17
  • Hi @Omar Tammam! Thank you! I have definitely to buy new eyeglasses because I didn't find just a simple missing dot `.` even if I read more times to find where it was wrong.... Thank you again! Now the error disappeared! – Luigino Jun 04 '22 at 09:45