insert the array into the database - mysql

I am getting this result when I am using graph api . it is in array format
{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}
I am working in a php language and phpmyadmin database . Now i want to insert the array into my database . Should i make a column for id , name , first_name ,last_name,link,favorite_teams etc or should i make a one column for all of this ........
how toinsert tha array into the database

Actually this is not an array. This is JSON. In JSON there are two formats,
JSONArray [ ]
JSONObject { }
You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode.
Go through this you will get idea.

Storing facebook app data in a database is against Facebook policy http://developers.facebook.com/policy/
$data = '{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}';
//decode to get as php variable
$values = json_decode($data,true); //true to decode as a array not an object
$sql = "INSERT INTO TableName (id,name,first_name,last_name,link,username)
VALUES ('".$values['id']."','".$values['name']."','".$values['first_name']."','".$values['last_name']."','".$values['link']."','".$values['username']."')";
mysql_query($sql);
Json_decode() takes a JSON encoded string and converts it into a PHP variable.

Related

Match and merge from multiple files with jq

I have a directory with a bunch of "logins" json files like this:
[
{
"user_id": "5ce722b803b54f03f745cdf45d579920",
"time": "2019-10-29T20:03:18.894006Z"
},
{
"user_id": "5ce722b858f3e80e6e85aad3113a1665",
"time": "2019-10-29T20:11:32.4843Z"
}
]
In another directory I have a bunch of "users" json files like this:
[
{
"id": "5ce722b803b54f03f745cdf45d579920",
"email": "foo#gmail.com",
"first_name": "John",
"last_name": "Doe",
"enabled": true,
"created_at": "2019-06-13T17:07:17.2925Z",
"updated_at": "2019-06-13T17:15:20.903085Z",
"groups": {
"count": 1,
"shortlist": [
{
"id": "5d0282c5d5d6063286140e864a0c6506",
"name": "cool users",
"description": "cool users",
"locked": true
}
]
},
"avatar": "",
"role_id": "5d0282c488bba9ebc62df8b3c38571a9",
"company_uid": ""
},
{
"id": "5d0284fdec62d47039e7119013b0aa2c",
"email": "bar#gmail.com",
"first_name": "Jane",
"last_name": "Doe",
"enabled": true,
"created_at": "2019-06-13T17:16:45.210018Z",
"updated_at": "2019-06-13T17:16:45.210018Z",
"groups": {
"count": 1,
"shortlist": [
{
"id": "5d0282c5d5d6063286140e864a0c6506",
"name": "cool users",
"description": "cool users",
"locked": true
}
]
},
"avatar": "",
"role_id": "5d0282c488bba9ebc62df8b3c38571a9",
"company_uid": ""
}
]
What I'm trying to do with jq is:
For each user_id in the "Logins" files, I want to find a matching id in the "Users" files.
I want to merge those two object together.
The intended outcome is another json file(s), which contains login and corresponding user data. As a bonus, I only want the email first and last name from "Users".
End result would be something like this:
[
{
"user_id": "5ce722b803b54f03f745cdf45d579920",
"time": "2019-10-29T20:03:18.894006Z",
"email": "foo#gmail.com",
"first_name": "John",
"last_name": "Doe"
}
}
I've tried variations of the below, but end up with what looks like an infinite loop or something. I know my for loops are wrong, just not sure how to work with multiple files like this.
lastlogins="/last10/*.json"
users="/users/*.json"
for ll in $lastlogins; do
for user in $users; do
userid=$(jq -r '.[].user_id' $ll)
jq -c --arg userid "$userid" '.[] | select(.id == $userid)' $user
done
done
There is no need to use shell looping. That is, everything can be done using jq.
For example, using the invocation:
jq -f users-logins.jq --argfile users users.json logins.json
where users-logins.jq contains:
INDEX($users[]; .id) as $udict
| map( if $udict[.user_id] then . + $udict[.user_id] else empty end)
| map( {user_id, time, email, first_name, last_name} )
the output using the sample inputs would be:
[
{
"user_id": "5ce722b803b54f03f745cdf45d579920",
"time": "2019-10-29T20:03:18.894006Z",
"email": "foo#gmail.com",
"first_name": "John",
"last_name": "Doe"
}
]

Using jq to return specific information in JSON object

I wish to parse individual elements of inner JSON object to build / load in the database.
The following is the JSON object. How can I parse elements like id, name queue etc? I will iterate it in loop and work and build the insert query.
{
"apps": {
"app": [
{
"id": "application_1540378900448_18838",
"user": "hive",
"name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
"queue": "Data_Ingestion",
"state": "FINISHED",
"finalStatus": "SUCCEEDED",
"progress": 100
},
{
"id": "application_1540378900448_18833",
"user": "hive",
"name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
"queue": "Data_Ingestion",
"state": "FINISHED",
"finalStatus": "SUCCEEDED",
"progress": 100
}
]
}
}
You're better off converting the data to a format easily consumed by a database processor, like csv, then do something about it.
$ jq -r '(.apps.app[0] | keys_unsorted) as $k
| $k, (.apps.app[] | [.[$k[]]])
| #csv
' input.json
its pretty simple just fetch elment which is having an array of values.
var JSONOBJ={
"apps": {
"app": [
{
"id": "application_1540378900448_18838",
"user": "hive",
"name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
"queue": "Data_Ingestion",
"state": "FINISHED",
"finalStatus": "SUCCEEDED",
"progress": 100
},
{
"id": "application_1540378900448_18833",
"user": "hive",
"name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
"queue": "Data_Ingestion",
"state": "FINISHED",
"finalStatus": "SUCCEEDED",
"progress": 100
}
]
}
}
JSONOBJ.apps.app.forEach(function(o){console.log(o.id);console.log(o.user);console.log(o.name);})

Perl: Combine two JSONs

I am looking for help/guidance to combine multiple JSON into 1 JSON based on a node in PERL. For example here are the two JSON content that I want to combine.
JSON #1:
{
"title": "All",
"lastModified": "2017-04-11T00:00:00.000+0000",
"users": [{
"name": "Alpha One",
"title": "Security Chief",
"locations": [{"id": "730WLCS"}, {"id": "943MCS"}]
},
{
"name": "Alpha Two",
"title": "Security Manager"
}
]
}
JSON #2:
{
"title": "All",
"lastModified": "2017-04-11T00:00:00.000+0000",
"users": [{
"name": "Beta One",
"title": "Architect",
"locations": [{"id": "730WLCS"}]
}
]
}
RESULT JSON :
{
"title": "All",
"lastModified": "2017-04-11T00:00:00.000+0000",
"users": [{
"name": "Alpha One",
"title": "Security Chief",
"locations": [{"id": "730WLCS"}, {"id": "943MCS"}]
},
{
"name": "Alpha Two",
"title": "Security Manager"
},
{
"name": "Beta One",
"title": "Architect",
"locations": [{"id": "730WLCS"}]
}
]
}
Basically, I want to combine only the "users" node.
I tried to get the node using from_json and tried to push into an array, but it is not working.
Here is the code that I tried:
my $json_obj1 = from_json($json_txt1, {utf8 => 1});
my $json_obj2 = from_json($json_txt2, {utf8 => 1});
push(#myJSON, #{$json_obj1->{'users'}});
push(#myJSON, #{$json_obj2->{'users'}});
Any help is much appreciated.
Thank you.
my $json_obj1 = decode_json($json_txt1); # Same as from_json($json_txt1, {utf8 => 1})
my $json_obj2 = decode_json($json_txt2);
push #{ $json_obj2->{users} }, #{ $json_obj1->{users} };
If you want to remove duplicates , keeping the newer records (assuming $json_obj1 is the older state), you can use the following:
my %seen;
#{ $json_obj2->{users} } =
grep !$seen{$_->{name}}++,
#{ $json_obj2->{users} },
#{ $json_obj1->{users} };

PowerShell- Merging JSON files

How to copy the objects of one array of a JSON file to an array of another JSON file using PowerShell? For Example I have one JSON file like:
"type": "Employee",
"Properties": [
{
"Name": "Raj",
"Id": "18111",
"email": "emp1#company.com",
"Position": "Manager",
"DateOfJoining": "16.10.14",
}
],
"Description": "Employee details"
and another JSON file as:
"type": "Employee",
"Properties": [
{
"Name": "Ram",
"Id": "44000",
"email": "emp2#company.com",
"Position": "Admin",
"DateOfJoining": "10.12.14",
},
{
"Name": "Paul",
"Id": "44002",
"email": "emp3#company.com",
"Position": "Programmer",
"DateOfJoining": "10.9.14",
},
],
"Description": "Employee details"
I want to copy the arrays from 1st JSON file to the 2nd JSON file.
You can try something like this:
$c1 = Convert-FromJson (gc file1.json -raw)
$c2 = Convert-FromJson (gc file2.json -raw)
$c3 = $c1.Properties + $c2.Properties
$c3 | ConvertTo-Json

ActiveRecord get array inside array with three tables

I'm trying to output a json string with the ActiveRecord system in the Codeigniter framework.
The right syntax for my json string need to be:
{
"data": [
[
{
"name": "xxxx",
"city": "xxx",
"address": "xxx",
"image": "xxx",
"marketId": "1",
"products": [
"Id": "36",
"productId": "36",
"price": "120",
"discounts": "1",
"title": "xxx",
"category": "2",
"weight": "12.5",
"code": "EA123",
"isUnitized": "0",
"description": "xxxx",
"changed": "2014-04-08 15:09:16",
"units": "xxx"
]
}
]
]
Pay attention to the "products" array.
But the string that i'm getting from the code is not right, here is the wrong string:
{
"data": [
[
{
"name": "xxx",
"city": "xxx",
"address": "xx x",
"image": "xxx",
"marketId": "1",
"Id": "36",
"productId": "36",
"price": "120",
"discounts": "1",
"title": "xxx",
"category": "2",
"weight": "12.5",
"code": "EA123",
"isUnitized": "0",
"description": "xxx",
"changed": "2014-04-08 15:09:16",
"units": "xxx"
}
]
]
You can see that the product array is't showing like an array but as a regular string inside the main array.
Here is the code that have I built:
$this->db->select('*');
$this->db->from('markets');
$this->db->where("markets.marketId", $marketId);
$this->db->join('linkedPrices', 'linkedPrices.marketId = markets.marketId');
$this->db->join('products', 'products.Id = linkedPrices.productId');
$this->db->order_by("linkedPrices.price", "DESC");
$output[] = $this->db->get()->result();
So you can see here the join between the table. The goal is to show the products tables as individual array inside the markets array as you can see at the top example of the json string.
No not this way you gonna have array of products foreach your dataset it can be done by looping over your results and fetch the relevant product data
$result=new stdClass();
$this->db->select('*');
$this->db->from('markets');
$this->db->where("markets.marketId", $marketId);
$this->db->join('linkedPrices', 'linkedPrices.marketId = markets.marketId');
$this->db->order_by("linkedPrices.price", "DESC");
$result = $this->db->get()->result();
foreach($result as $r){
$result->products=$this->db->select('*')
->from('products')
->where('id',$r->productId)
->get()
->result();
}
$output[]=json_encode($result);