How to load JSON into MySQL, including JSON specific fields [closed]

2

Can anyone help me with a question that is killing me?

First, I'm trying to load this JSON into MySQL

{
    "business_id": "fNGIbpazjTRdXgwRY_NIXA",
    "full_address": "1201 Washington Ave\nCarnegie, PA 15106",
    "hours": {

    },
    "open": true,
    "categories": ["Bars",
                   "American (Traditional)",
                   "Nightlife",
                   "Lounges",
                   "Restaurants"],
    "city": "Carnegie",
    "review_count": 5,
    "name": "Rocky's Lounge",
    "neighborhoods": [],
    "longitude": -80.084941599999993,
    "state": "PA",
    "stars": 4.0,
    "latitude": 40.396468800000001,
    "attributes": {
        "Alcohol": "full_bar",
        "Noise Level": "average",
        "Music": {
            "dj": false,
            "background_music": true,
            "karaoke": false,
            "live": false,
            "video": false,
            "jukebox": false
        },
        "Attire": "casual",
        "Ambience": {
            "romantic": false,
            "intimate": false,
            "touristy": false,
            "hipster": false,
            "divey": false,
            "classy": false,
            "trendy": false,
            "upscale": false,
            "casual": false
        },
        "Good for Kids": true,
        "Wheelchair Accessible": false,
        "Good For Dancing": false,
        "Delivery": false,
        "Coat Check": false,
        "Smoking": "no",
        "Accepts Credit Cards": true,
        "Take-out": false,
        "Price Range": 2,
        "Outdoor Seating": false,
        "Takes Reservations": false,
        "Waiter Service": true,
        "Caters": false,
        "Good For": {
            "dessert": false,
            "latenight": false,
            "lunch": false,
            "dinner": false,
            "brunch": false,
            "breakfast": false
        },
        "Parking": {
            "garage": false,
            "street": false,
            "validated": false,
            "lot": false,
            "valet": false
        },
        "Has TV": true,
        "Good For Groups": true
    },
    "type": "business"
}

And I'm using the following code

<?php
try 
{
    $conn = new PDO("mysql:host=localhost:118;dbname=mydb", "root", "1234");
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}

//read the json file contents
$jsondata = file_get_contents('c:\yelp_academic_dataset_business.json');

ini_set('memory_limit', '512M');
//convert json object to php associative array
$data = json_decode($jsondata, true);

//get the employee details
$idBusiness = $data['business_id'];
$name = $data['name'];
$neighborhoods = $data['neighborhoods'];
$full_address = $data['full_address'];
$city = $data['city'];
$state = $data['state'];
$latitude = $data['latitude'];
$longitude = $data['longitude'];
$stars = $data['stars'];
$review_count = $data['review_count'];
$open = $data['open'];
$procedure = $conn -> prepare("INSERT INTO business(business_id, name, neighborhoods, full_address, city, state, latitude, longitude, stars, review_count, open)
VALUES('$idBusiness', '$name', '$neighborhoods', '$full_address', '$city', '$state', '$latitude', '$longitude', '$stars', '$review_count', '$open')");
$procedure -> execute(); 
?>

I'm giving error in business_id, it says that it can not insert, is something wrong with the code? or will it be in the relational model that the error is? the error is as follows:

  

Fatal error: Uncaught exception 'PDOException' with message   'SQLSTATE [42S22]: Column not found: 1054 Unknown column' business_id '   in 'field list' 'in C: \ wamp \ www \ Yelp_mysql_Business.php on line 50

Second, I have 2 tables in the relational model with the name, " complex attributes " which contains the id and the Designation and "simple attributes" that contain the id, >

I wanted a way to be able to save the value "good_for", "music" etc ... in the field of the complex attributes table that unfold in simple attributes as in the case of "music", dj, karaoke, etc.

Only with the code above only allows me to save the value of JSON and not the same attributes.

For example, for the attributes you can not load like this for the relational model so I had to create 2 tables related to the Business table, "Complex Attributes" that are those that have several attributes within this as is the case of Music, Ambience, and "Simple Attributes" such as alcohol, noise level. In simple attributes I created 2 fields, designation and value, which translates into alcohol being the designation and full bar the value. My question is how to split the data so that alcohol and these simple attributes are loaded into the designation field in the "Simple Attributes" table in MySQL.

This is my relational model, there are other JSON like Tip, Review, User and Check-in, but knowing how to load it into the business and how to solve the problem of the attributes later on for the rest, p>

Someone can help me please, it's important.

    
asked by anonymous 30.07.2015 / 04:45

1 answer

2

The error you're getting:

  

"Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42S22]: Column not found: 1054 Unknown column' business_id 'in' field list '' in C: \ wamp \ www \ Yelp_mysql_Business.php on line 50 "

It's because of this line here:

$procedure = $conn->prepare("INSERT INTO business(business_id, name,
 neighborhoods, full_address, city, state, latitude, longitude, stars,
 review_count, open)
VALUES('$idBusiness', '$name', '$neighborhoods', '$full_address', '$city',
 '$state', '$latitude', '$longitude', '$stars', '$review_count', '$open')");

Notice that the first row is using business_id as the column name, but the column name is idBusiness . I think it was just a confusion / conflict between the ID name in JSON and MySQL.

Then modify your SQL to look like this:

INSERT INTO business(idBusiness, name, ... )
VALUES('$idBusiness', '$name', ... )
    
30.07.2015 / 06:20