Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to parse big JSON file and insert into database using php?

Tags:

json

php

mysql

I have this huge JSON file. Currently the way I am using it is:

  1. Read the entire contents into a string
  2. Do json_decode, get the array
  3. Loop through the array one record at a time and build my SQL insert statement

Problem is, the code is ugly. Also, some of the objects in these arrays are themselves arrays, and not all records contain all values. I have to use isset to check if a particular value is present, or use a default value etc. Overall, it works correctly, but the code is ugly. Is there any way I can write this better?

like image 450
user61734 Avatar asked Feb 02 '10 10:02

user61734


People also ask

How read API data and insert it into MySQL using PHP?

you can do it by decoding the data into an array, looping over the array and then constructing an INSERT statement with a clause for each row of the array, and then executing that statement.


2 Answers

If you dont want to write ugly code to generate sql queries consider using an ORM Propel and doctrine are the ones i have personally used.

doctrine has a method to generate an object from an array and then simply call a save method on it.

have a look at this http://www.doctrine-project.org/documentation/manual/1_0/en/working-with-models:arrays-and-objects:from-array

like image 191
Yashvit Avatar answered Oct 10 '22 00:10

Yashvit


Hard to advice without looking at the code, but I'd like to remind you about the PHP + operator for arrays, which merges two arrays non-destructively.

$default_values = array('Name' => '', 'Email' => '', 'Meta' => '');
$data = array('Name' => 'John'); // Only name set
$fixed_data = $data + $default_values;

$fixed_data now looks like array('Name' => 'John', 'Email' => '', 'Meta' => ''); without a need for multiple isset() checks. (Might not be applicable to your case, hard to say without more info.)

like image 43
kb. Avatar answered Oct 10 '22 01:10

kb.