Jump to content

Help to get Json data to a comma delimited file


techboy992

Recommended Posts

Hi there I have a bunch of json data from a old database backup that I would like to get in to a comma delimited file for mySQL insert.

 

Try to see attached file:

 

What I wan to archive is to get a script that can convert the data in to a sql file that I can be executed by PHP to insert it to a mysql Database

 

Hop some one can help me

 

Thanks in advance

result.txt

Edited by techboy992
Link to comment
Share on other sites

  • json_decode to put it in an array.
  • loop through the array
  • trim tabs and spaces from field values
  • write to csv file
$arr = json_decode(file_get_contents('result.txt'),1);  // create array feom json data
$arr = array_filter($arr);  // remove empty records
$fp = fopen('result.csv','w');  // open output file
foreach ($arr as $record) {
    $rec = array_map('trim', $record);
    fputcsv($fp,$rec);
}
fclose($fp);


Your currency values should not be stored in a DB with currency symbols and comma formatting so you have some extra work to do there.

Link to comment
Share on other sites

Hi Barand

 

I now have the solution to remove the '$' in the records:

foreach ($arr as $record) {
	$record = str_replace('$','',$record);
	$record = str_replace(' ','',$record);	
	$rec = array_map('trim', $record);
    fputcsv($fp,$rec);
}

so how can I now insert the records into the database using mySQLI ?

 

Thanks in advance

Brian

Edited by techboy992
Link to comment
Share on other sites

Updated solution from Barand

// create array from json data
$arr = json_decode(file_get_contents('result.txt'),1);
// remove empty records
$arr = array_filter($arr);
// open output file
$fp = fopen('result.csv','w');
foreach ($arr as $record) {
    // trim the values
    $rec = array_map('trim', $record);
    // skip records that only contain " "
    if($rec[0]==' ') { continue; }
    // remove non-numeric characters from amount
    $rec[2] = preg_replace("#[^\d]#", "", $rec[2]);
    // replace character code with apostrophe
    $rec[3] = str_replace(''', "'", $rec[3]);
    // save in output file in csv format
    fputcsv($fp, $rec);
}
// close output file
fclose($fp);
Edited by Psycho
Link to comment
Share on other sites

Hi Barand and Psycho

 

I have problem with that load file to get it right

 

I have try this but it is not inserting anything in the database:

$sql = "LOAD DATA INFILE 'result.csv' INTO TABLE playernames
  FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
  LINES TERMINATED BY '\r\n' 
(playername,location,winnings,mostcash)";


// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
};

$result = mysqli_query($con, $sql);

if (mysqli_affected_rows($con) == 1) {
  $message = "The data was successfully added!";
} else {
  $message = "The user update failed: ";
  $message .= mysqli_error($con); 
};
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.