Code Monkey home page Code Monkey logo

json-to-mysql's Introduction

json-to-mysql

Easily take any JSON object and create+insert it into a mysql table built from its structure. Also search, update, index, and validate tables with JSON.


include("config.php");
include("include.classloader.php");

$classLoader->addToClasspath(ROOT);


$mysql = new MySQLConn(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASS);

$db = new JSONtoMYSQL($mysql);

// create some json
$obj = json_decode('{"id":4,"name" : "asfd"}');

// save it to a table
$db->save($obj, "brandnewtable");

// SELECT * from brandnewtable WHERE id = 4

$obj = $db->table("brandnewtable")->find(["id" => 4]);

print_r($obj);

// SELECT * FROM brandnewtable WHERE id > 4 ORDER BY name DESC

$obj = $db->table("brandnewtable")->find(["id" => 4], ["id" => ">"], ["name DESC"]);

print_r($obj);

// SELECT * FROM brandnewtable WHERE id IN (4, 5, 6, 7)

$obj = $db->table("brandnewtable")->find(["id" => [4, 5, 6, 7]]);

print_r($obj);


Support the project

Has json-to-mysql saved you some time? Become a Github Sponsor and buy me a coffee ☕️ 😄

json-to-mysql's People

Contributors

adamwulf avatar hubdotcom avatar justdevzero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-to-mysql's Issues

DIRECTORY_SEPARATOR

Hi there,

I just wanted to do a quick test under windows, and your path separators won't work here. You might want to use DIRECTORY_SEPARATOR instead of "/" in the future, to build your strings.

Goal 2: validate table structure

Given a JSON object, validate the construction of a mysql table, including sub-tables, foreign keys, and data types. Return a JSON object describing what edits should be made (new columns, change types, add NULLABLE, etc) to the existing table if any

need to handle array (object) subdata

How to receive subdata Events?

{ "CommandStatus": "0", "Events": [ { "ActivityId": "1" },{ "ActivityId": "2" },...,{ "ActivityId": "867" } ], "EventsCount": "867" }

DatabaseException class not found; also mysql_connect (and similar are dropped from PHP)

Greetings:
I'd really like the approach of this project, but I am unable to utilize it, at its present form.
It does have potentials, however, to make a GREAT Project!
I was looking for a way to import any JSON into an SQLite3, but this should work, just as well.

JSON that I am trying to import is like this:
{
"stations":[
{
"id":"WYNI1",
"name":"DALE BITNER",
"elev":"6391",
"latitude":"43.049722",
"longitude":"-111.379444",
"state":"ID",
"distance":"",
"provider":"RAWS",
"link":"http://www.wrh.noaa.gov/mesowest/getobext.php?sid=WYNI1",
"Date":"9:45 pm MDT Sep 26, 2014",
"Age":"0.55",
"Temp":"59",
"Dewp":"46",
"Relh":"63",
"Wind":"NW@8",
"Direction":"309°",
"Winds":"8",
"Windd":"309",
"Gust":"26",
"AccPrecip":"14.67",
"FuelMoist":"9.8",
"FuelTemp":"58",
"Ceiling":""},
{
"id":"690",
"name":"Central Facilities",
"elev":"4950",
"latitude":"43.5326",
"longitude":"-112.94775",
"state":"ID",
"distance":"",
"provider":"ARL FRD",
"link":"http://www.wrh.noaa.gov/mesowest/getobext.php?sid=690",
"Date":"10:00 pm MDT Sep 26, 2014",
"Age":"0.30",
"Temp":"57",
"Dewp":"53",
"Relh":"88",
"Wind":"E@12",
"Direction":"79.6°",
"Winds":"12",
"Windd":"79.6",
"Gust":"16",
"a5minPrecip":"0.00",
"Ceiling":""}]}

handle sub object and subsubobject data

when inserting data into a table, allow it to have data in sub objects, and create normal keys in the original object for that data. so:

{ "properties" : { "one" : true, "two" : false} }

becomes:

{ "properties_one" : true, "properties_two" : false}

Example typo and bug in line 208

  1. bug in code
    Line 208 in ExistingMYSQLTable.php should use variable $val instead of $value. You do not iterate with the correct element used.

  2. Your example in README.md has a typo
    $obj = $db->table("brandnewtable")->find(["id" => [4, 5, 6 7]]); should be
    $obj = $db->table("brandnewtable")->find(["id" => [4, 5, 6, 7]]); note the missing comma.

Import nested JSON sub-arrays

Importing JSON with multiple sub-arrays nested to different depths fails with 'need to handle sub-array's' message.

example.php does not work

when I run:
$ php example.php

I get:

PHP Notice: Undefined variable: classLoader in /home/sbash/json-to-mysql/example.php on line 10
PHP Fatal error: Call to a member function addToClasspath() on null in /home/sbash/json-to-mysql/example.php on line 10

I only edited the file to add <?php as short tags do not work in my environment. I get this on php 7.x and 5.6...

I have tested this on a clean 'git clone ...' just making the short tag modification above to no avail...

handle custom primary key

when creating new tables, allow creation of a custom primary key, including custom type. i assume its possible for a custom primary key of varchar (?) or text (?).

Goal 1 Notes: Create Table and Insert Data

The idea is to take a json object, and insert auto-generate the table and/or columns necessary to insert the data into mysql.

given a json object and target table name:

  1. if the table doesn't exist then:
    • loop through the json object, collecting type information for each key value
    • generate a table with the table name and columns
    • start back at (1)
  2. else, if the table exists, then:
    • verify that the table contains columns for all of the keys in the json object
      • if not, then add the appropriate columns (never delete/rename), start back at 2
    • determine the unique key of the json object "id" unless otherwise specified
    • determine if a row with that id already exists,
      • if so, then update the row with the json object
      • otherwise insert a new row

if the JSON object contains other objects as its property values, then flatten them into columns, so:
{ "foo" : { "bar1" : "baz1", "bar2" : "baz2"} }
is equivalant to:
{"foo_bar1" : "baz1", "foo_bar2" : "baz2" }

if a property contains an array for a value, then each of those objects should be added to their own mysql table, with a foreign key to the original table's row. so:
{ "foo" : [{ "bar" : "baz1"}, {"bar" : "baz2"}] }

would auto generate a table tablename_foo, with foreign key column: foo_id that points to the original foo object. tablename_foo would also contain the data column bar, and 2 rows for each baz1 and baz2 would be inserted.

JSON-Data won't be processed completely (ignores sub-arrays)

Hi there,

I'm parsing data from the youtube API. In this case I got an array of items I'd like to parse:
this is the input for the json_decode

String input for json_decode:

'{
"title": "Dave Gaming",
"description": "Willkommen zu meinem Channel mit der besten Community weit und breit! \n\nHier findet ihr alles, was das Gamerherz begehrt. 20 Jahre alt und seit 17 Jahren leidenschaftlicher Gamer!\n \nGespielt wird alles, was Spaß macht:\n\nAktuell:\n- F1 2015 - GTA ONLINE - League of Legends - MAD MAX - FIFA 16 - Tearaway Unfolded - Forza 6 - SOMA\n\nKommende Let's Plays:\n\n\nOKTOBER:\nUncharted Collection - Assassin's Creed: Syndicate - HALO 5\n\nNOVEMBER:\nNeed for Speed - Call of Duty: Black Ops 3 - Rise of the Tomb Raider - Star Wars: Battlefront 3\n\nDEZEMBER:\nJust Cause 3\n \n \nAuf eine geile gemeinsame Zeit! :)",
"publishedAt": "2014-10-17T21:04:42.000Z",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s88-c-k-no/photo.jpg"
},
"medium": {
"url": "https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s240-c-k-no/photo.jpg"
},
"high": {
"url": "https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s240-c-k-no/photo.jpg"
}
},
"localized": {
"title": "Dave Gaming",
"description": "Willkommen zu meinem Channel mit der besten Community weit und breit! \n\nHier findet ihr alles, was das Gamerherz begehrt. 20 Jahre alt und seit 17 Jahren leidenschaftlicher Gamer!\n \nGespielt wird alles, was Spaß macht:\n\nAktuell:\n- F1 2015 - GTA ONLINE - League of Legends - MAD MAX - FIFA 16 - Tearaway Unfolded - Forza 6 - SOMA\n\nKommende Let's Plays:\n\n\nOKTOBER:\nUncharted Collection - Assassin's Creed: Syndicate - HALO 5\n\nNOVEMBER:\nNeed for Speed - Call of Duty: Black Ops 3 - Rise of the Tomb Raider - Star Wars: Battlefront 3\n\nDEZEMBER:\nJust Cause 3\n \n \nAuf eine geile gemeinsame Zeit! :"
},
"country": "DE"
}'

output of json_decode():

stdClass Object ( [title] => Dave Gaming [description] => Willkommen zu meinem Channel mit der besten Community weit und breit! Hier findet ihr alles, was das Gamerherz begehrt. 20 Jahre alt und seit 17 Jahren leidenschaftlicher Gamer! Gespielt wird alles, was Spaß macht: Aktuell: - F1 2015 - GTA ONLINE - League of Legends - MAD MAX - FIFA 16 - Tearaway Unfolded - Forza 6 - SOMA Kommende Let's Plays: OKTOBER: Uncharted Collection - Assassin's Creed: Syndicate - HALO 5 NOVEMBER: Need for Speed - Call of Duty: Black Ops 3 - Rise of the Tomb Raider - Star Wars: Battlefront 3 DEZEMBER: Just Cause 3 Auf eine geile gemeinsame Zeit! :) [publishedAt] => 2014-10-17T21:04:42.000Z [thumbnails] => stdClass Object ( [default] => stdClass Object ( [url] => https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s88-c-k-no/photo.jpg ) [medium] => stdClass Object ( [url] => https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s240-c-k-no/photo.jpg ) [high] => stdClass Object ( [url] => https://yt3.ggpht.com/-80u0cN0eJrY/AAAAAAAAAAI/AAAAAAAAAAA/y5b34eyGCrM/s240-c-k-no/photo.jpg ) ) [localized] => stdClass Object ( [title] => Dave Gaming [description] => Willkommen zu meinem Channel mit der besten Community weit und breit! Hier findet ihr alles, was das Gamerherz begehrt. 20 Jahre alt und seit 17 Jahren leidenschaftlicher Gamer! Gespielt wird alles, was Spaß macht: Aktuell: - F1 2015 - GTA ONLINE - League of Legends - MAD MAX - FIFA 16 - Tearaway Unfolded - Forza 6 - SOMA Kommende Let's Plays: OKTOBER: Uncharted Collection - Assassin's Creed: Syndicate - HALO 5 NOVEMBER: Need for Speed - Call of Duty: Black Ops 3 - Rise of the Tomb Raider - Star Wars: Battlefront 3 DEZEMBER: Just Cause 3 Auf eine geile gemeinsame Zeit! : ) [country] => DE ) when I call $db->save($obj, "channel");

where will be only the fields:
id
title
description
publishedAt
country

Sub-Elements will currently be ignored without any warning.
It would be great if you could add/insert into another table for each sub element and handle the mapping.

Your software doesn't work

I am getting a NULL value from the class, but all files are found.
The return JSON object is fully populated with data.

<title>Analytics</title>

Dump by ID

addToClasspath(ROOT); $mysql = new MySQLConn(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASS); $db = new JSONtoMYSQL($mysql); // create some json $obj = json_decode($output); // save it to a table $db->save($obj, "brandnewtable"); ?>

Enter the Object ID you would like for me to retrieve 'hearts' for.


Object ID :

Goal 3: Search table with JSON object

easily search for data matching an input JSON object. figure out how to easily support wildcard text search, and < > for numeric and date columns

Goal 4: Auto-Index

Try to auto-create indexes on columns based on the JSON data that populates the table, and based on the searches on a table. If a table is consistently searched on its start_date column, then auto-add an index on that column. Same for multi-column indexes.

"search, update, index, and validate tables with JSON" - How?

The example given works beautifully - however, when adding the same JSON data, the $db->save(...) method creates a duplicate in the resulting table. Is it possible to check for the row first and avoid duplicated entries - ie. if (!$db->find($json_object,"table") ...?

If someone could "flesh out" the example to include any other functions (search, update...), it would be hugely appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.