Forum Moderators: coopster

Message Too Old, No Replies

Proper Syntax for Inserting JSON files into mysql JSON datatype

         

Gilead

3:44 pm on Oct 18, 2018 (gmt 0)

10+ Year Member



I'm trying to get JSON data into a JSON table in MYSQL, but so far I'm only frustrating myself. I was able to get the JSON data formatted, but there is an error on the insert. Can someone give me a hand with this?

$id= $_POST['ID'];
$servicedate=$_POST['ServiceDate'];
$servicelocation=$_POST['ServiceLocation'];
$mileage=$_POST['Mileage'];
$labor=$_POST['Labor'];
$oilbrand=$_POST['OilBrand'];
$oilprice=$_POST['OilPrice'];
$filterbrand=$_POST['FilterBrand'];
$filterprice=$_POST['FilterPrice'];
$oilfilterpurchaselocation=$_POST['PurchasePlace'];

echo "This is the input received...";

//add true for associative array...
$arr = array('Service Date' => $servicedate, 'Service Location' => $servicelocation, 'Mileage' => $mileage, 'Labor' => $labor, 'Oil Brand' => $oilbrand, 'Oil Price' => $oilprice, 'Filter Brand' => $filterbrand , 'Filter Price' => $filterprice , 'Purchase Place' =>$oilfilterpurchaselocation);

$json= json_encode([$id => $arr]); Since my schema has a number of JSON tables, I want the ID to be the key for the array as an added layer of protection against inserting the wrong data in the wrong place.
echo 'Here is our new JSON';
print_r($json);


$sql= "INSERT INTO vehicle (OilChange) VALUES (JSON_OBJECT($json))";
$result=mysqli_query($ms, $sql);
if(!$result)
{
echo 'Oil Change Not Inserted! '. mysqli_error($ms);
}
else
{
echo "Oil Change Inserted!";
}


Thanks!

justpassing

4:08 pm on Oct 18, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



What is the error message?

Gilead

4:16 pm on Oct 18, 2018 (gmt 0)

10+ Year Member



Oil Change Not Inserted! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"1999NissanSentra":{"Service Date":"9\/11\/2018","Service Location":"Granite Tow' at line 1

NickMNS

4:31 pm on Oct 18, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would like to make one comment, that doesn't answer the question per se. I am a big proponent of no-sql databases, these are designed specifically with JSON in mind as all the data is stored as key value pairs as in JSON. There are certain drawbacks to using no_sql, in my view they are mostly over blown. One of the biggest draw backs is the lack of normalization of the data.

To me, inserting JSON into an SQL db is totally bonkers. Now not only will one have non-normalized data but the data is then practically not searcheable. If you want to add the JSON to your SQL db then deconstruct the JSON into its parts and create the appropriate tables. If you want to stick to using the JSON (which in my view is the smart thing to do) consider opting for a noSQL db, or a db that can handle both such as PostGreSQL.

Gilead

4:48 pm on Oct 18, 2018 (gmt 0)

10+ Year Member



Nick- I already gave noSQL a shot with google's firebase. I would have had to use their API to accomplish what I needed, unfortunately I couldn't find any tutorials on the subject, so I gave up on the idea. That's why I'm trying to make this work.