Update Users Laravel Eloquent Active Directory JSON - json

I am trying to update a Laravel Users table with eloquent from an Active Directory JSON dump. I have gotten as far as parsing out the JSON and removing user records from MYSQL that are in MYSQL but not in the JSON. I'm struggling to figure out how to Insert new records from the JSON that are not in the MYSLQ / Update existing records in the MYSQL that have changed in the JSON.
Here is my code so far
//Read AD Users JSON File
$strJsonFileContents = file_get_contents(public_path('ADusers.json'));
//Remove UTF8 Bom Characters and strip slashes
$bom = pack('H*','EFBBBF');
$strJsonFileContents = preg_replace("/^$bom/", '', $strJsonFileContents);
//run twice for double slashes
$strJsonFileContents = stripslashes($strJsonFileContents);
$strJsonFileContents = stripslashes($strJsonFileContents);
$strJsonFileContents = str_replace('/', '', $strJsonFileContents);
//JSON to Object
$decoded_json = json_decode($strJsonFileContents);
//Get list of EMPIDs from JSON into array to remove from database
$id_list = implode(",", array_map(function ($val) { return (int) $val->extensionAttribute2; },$decoded_json));
$id_list_arr = explode (",", $id_list);
//Delete records from database that are not in JSON Array
$numDelete = DB::table('users')->select()->whereNotIn('extensionAttribute2', $id_list_arr)->delete();
I also realize that the DB::table('users') is probably not quite the right way, so if somebody can help me figure out how to use the User model and how to update/insert from the JSON to the MYSQL, I would so greatly appreciate it.

Related

CSV Parser, how to auto discover col names & types

I am loading CVS files for data import. The files come from various sources so the header names and location are often changing. I searched and found helpful libs like CsvHelper & FileHelpers
Question: either using FileHelper.net or CsvHelper, how do we extract both the Header names & the Column datatype? so that I can create a drop for each col, to map between .NET type <==> to a SQL type
Just read in the first line of the file with, say,
string headers = File.ReadLines("MyFile.txt").First();
And then use a class builder to build whatever CSV spec you need.
DelimitedClassBuilder cb = new DelimitedClassBuilder("MyProduct", delimiter: ",");
cb.AddField("Name", typeof(string));
cb.LastField.TrimMode = TrimMode.Both;
cb.AddField("Description", typeof(string));
cb.LastField.FieldQuoted = true;
cb.LastField.QuoteChar = '"';
cb.LastField.QuoteMode = QuoteMode.OptionalForBoth;
// etc... e.g., add a date field
cb.AddField("SomeDate", typeof(DateTime));
engine = new FileHelperEngine(cb.CreateRecordClass());
DataTable dt = engine.ReadFileAsDT("test.txt");

store string into array

In MySQL, I have a field name postcodes and the type is LONGTEXT. It stores several postcodes being separated by comma. How would I retrieve that and store it as an array for other use ?
you can use the PHP method explode().
one think you can't do, is to do a where x = x on it in the database.
In the model, you can set the mutator methods:
public function getPostcodesAttribute($value) {
return explode(',',$value);
}
public function setPostcodesAttribute($value) {
$this->attributes['postcodes'] = implode(',',$value);
}
Lets say that you have the result stored in a string like this:
$s = "6000,5447"; //$s = $array->postcodes;
you can get the each value on an index in an array using this:
$values= explode(",", $s);
echo $values[0]; // 6000
Or even better.. you can store it as json, and retrieve it as json in array format.
Store it as a JSON field in MySQL, Laravel encode and decode them when you retrieve and save them respectively
in your migration
$table->json('field_name');
then in the model
protected $json = ['field_name'];
then whenever you access the field, laravel will convert it to an array for you, you don't have to call any explicit methods.
Doc - https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting
// the final array all the post codes are collected.
$postCodes = [];
foreach (Model::pluck('postcodes') as $stringCodes)
foreach (explode(',', $stringCodes) as $postCode) $postCodes[] = $postCode;

Regarding JSON to CSV

I am trying to read a JSON file and convert it to a .csv file and I got this error.
employee_parsed = json.loads('E:/Masters_Materials/Data_Science/Food/train.json')
emp_data = employee_parsed['train']
# open a file for writing
employ_data = open('E:/Masters_Materials/Data_Science/Food/train.csv', 'a')
# create the csv writer object
csvwriter = csv.writer(employ_data)
count = 0
for emp in emp_data:
if count == 0:
header = emp.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(emp.values())
employ_data.close()
Json should be in homogeneous collection.just write it into http response after setting the response type csv
you need to create a header of json object key in comma separated manner.
then create row by getting respective key values in comma separated manner.

JSON validator validates the JSON but my left hand tags are not appearing

i am developing an iOS app and this app fetches data using JSON from mysql database. i have configured the json properly so far. right hand $outputs are ok and json validator validated the json formatting is completely ok. but my left hand tags are not appearing some how. here i m providing the code what i did. For this app publishing is on hold for long time.
//fetch data from current month table
$q = mysql_query("SELECT * FROM $monthyear where news_date = '$date'");
if (!$q) {
die('Invalid query executed: ' . mysql_error());
}
while($e=mysql_fetch_row($q)){
$output[]=$e;
$responce[$e]['news_id'] = $output['news_id'];
$responce[$e]['news_title'] = $output['news_title'];
$responce[$e]['news_reporter'] = $output['news_reporter'];
$responce[$e]['news_details'] = $output['news_details'];
$responce[$e]['photo'] = $output['photo'];
$responce[$e]['path'] = 'admin/'.str_replace('\\','',$output['path']);
$responce[$e]['menu_id'] = $output['menu_id'];
$responce[$e]['menu_type'] = $output['menu_type'];
$responce[$e]['news_publish_status'] = $output['news_publish_status'];
$responce[$e]['news_order'] = $output['news_order'];
$responce[$e]['news_date'] = $output['news_date'];
$responce[$e]['news_time'] = $output['news_time'];
$responce[$e]['added_by'] = $output['added_by'];
$responce[$e]['directory'] = $output['directory'];
$responce[$e]['read_number'] = $output['read_number'];
$responce[$e]['comment_number'] = $output['comment_number'];
$responce[$e]['news_comment_table_name'] = $output['news_comment_table_name'];
}
echo(json_encode($output));
I am not getting any way to show left hand tags though it exists in the script. can any one help me on this by guiding or giving an example from existing source code after modifications. TIA
Ishtiaque
You are using $e which is an array as an array key in $response[$e], and this is not allowed in PHP:
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
I think you want to use the ID as a key.

json encode error when adding a new column in sql

I have a query:
"SELECT Time, Date, Name, Email FROM table"
It converts the results to json to be passed via ajax, the problem is I want a new column in the sql, so I add it to the query:
"SELECT Time, Date, Name, Email, Address FROM table"
now the json encode does not work, I have tried changing data types and using UTF-8 however this did not work, none of the others are using UTF-8 but still work anyway, Thanks.
This is my code to encode to json which does work until I add the new collumn from sql
if ($result = $mysqli->query($query)) {
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
}
Solved
The problem was the last column i was trying to get was called "Show" for some reason sql does not like this, i renamed this column to "lol" (temporary) and it works!