Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I dynamically create a mySQL table from JSON?

Let's say I have a server side script which generates JSON from a simple select on a table. The JSON is encoded in the 1st script.

I have no control over this 1st script, but am aware when the underlying database structure changes and when the JSON structure changes.

Script 2 uses CURL to get the .js file (contents) which contains the JSON, I can then decode into an array.

What I need to do then is store the data in another database.

My question is basically about automating this process and being able to create a table from an array when you don't know what the structure of the array is until it arrives.

Can it be done?

EDIT Added the JSON as it currently stands, but the point is it might change.

{"name": "Google",
 "homepage_url": "http://www.google.com",
 "blog_url": "",
 "blog_feed_url": "",
 "twitter_username": "",
 "category_code": "ecommerce",
 "tag_list": "retail-portal-online-shopping-markets",
 "alias_list": null,
 "image": null,
 "products":
  [],
 "relationships":
  [],
 "competitions":
  [],
 "providerships":
  [{"title": "Legal",
    "is_past": false,
    "provider":
     {"name": "TaylorWessing",
      "permalink": "taylorwessing"}}],
 "offices":
  [{"description": "European HQ",
    "address1": "",
    "address2": "",
    "zip_code": "",
    "city": "Brussels",
    "state_code": null,
    "country_code": "BEL",
    "latitude": null,
    "longitude": null}]}
like image 577
T9b Avatar asked Sep 01 '25 09:09

T9b


2 Answers

Simple code piece to create a mysql table and insert values from any JSON variable. This is a fast hack.. check field types etc :) There's probably better ways, but this one work and has already saved me hours in manual field-naming

This can be modified to create a relation table handling objects aswell. Now, it's made for array formed JSON stuff and not objects ie arrays in arrays.

<?php

    JSON_to_table($place_your_JSON_var_here_please);

            function JSON_to_table($j_obj, $tblName = "New_JSON_table_" . time()){
            $j_obj = json_decode($your_JSON_variable, true);
            if(!mysql_num_rows( mysql_query("SHOW TABLES LIKE '" . $tblName . "'"))){ 
                $cq = "CREATE TABLE ". $tblName ." (
                id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,";
                foreach($j_obj as $j_arr_key => $value){
                    $cq .= $j_arr_key . " VARCHAR(256),";
                }
                $cq = substr_replace($cq,"",-1);
                $cq .= ")";
                mysql_query($cq) or die(mysql_error());
            }

            $qi = "INSERT INTO $tblName (";
            reset($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= $j_arr_key . ",";
                }
                $qi = substr_replace($qi,"",-1);
            $qi .= ") VALUES (";
            reset($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= "'" . mysql_real_escape_string($value) . "',";
                }
            $qi = substr_replace($qi,"",-1);
            $qi .= ")";
            $result = mysql_query($qi) or die(mysql_error());

        return true;
            }
?>
like image 184
K. Kilian Lindberg Avatar answered Sep 03 '25 21:09

K. Kilian Lindberg


I will answer next to the question but if you want to store JSON why not use a document database? CouchDB couch store your JSON "as is" without you having to do any processing.

like image 43
RageZ Avatar answered Sep 03 '25 23:09

RageZ