How can I embed repeated data into single JSON object - mysql

How can I embed below data in JSON format in such way that, I can get only one JSON object for each hall.
As you can see I have data from two tables one is from "hall_information" that is not repeated in table because hall_name ,service_provider_id & id will be entered once by user.
But in second table that "hall_price" in which i have slot_name, slot_ending_time, slot_starting_time, hall_type, which are repeated as user can describe his price for hall for morning, evening and full day, and for hall_type mini, mantap and bunquet. so these rows can repeat 9 times for each hall with 3 hall_types and for each hall_type 3 pricing.
So i want to combine into only one row as a query result in json format. I am using php with Codeigniter & angular.js for my project. how to embed this result of query into single json object. so that while looping using ng-repeat i can get only one hall NOT 9 halls with same name. Thanks in advance.
var hallinfo =
[
{
"hall_name":"Sai mantap","id":"12","service_provider_id":"35",
"slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
"hall_info_id":"45","hall_type":"mantap"
},
{
"hall_name":"Sai mantap","id":"12","service_provider_id":"35",
"slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"45","hall_type":"mini"
},
{
"hall_name":"Sai mantap","id":"12","service_provider_id":"35",
"slot_name":"full_day","slot_charge":"15000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"45","hall_type":"bunquet"
},
{
"hall_name":"Raman mantap ","id":"13","service_provider_id":"36",
"slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
"hall_info_id":"46","hall_type":"mantap"
},
{
"hall_name":"Raman mantap","id":"13","service_provider_id":"36",
"slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"46","hall_type":"mini"
},
{
"hall_name":"Raman mantap","id":"13","service_provider_id":"36",
"slot_name":"full_day","slot_charge":"25000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"46","hall_type":"bunquet"
},
];
here are the table schemas.
CREATE TABLE hall_information (
id integer NOT NULL,
hall_name character varying(30),
service_provider_id REFERENCES service_provider(id);
);
CREATE TABLE hall_pricing (
id integer NOT NULL,
slot_name slot_type NOT NULL,
slot_charge integer NOT NULL,
slot_starting_time time without time zone NOT NULL,
slot_ending_time time without time zone NOT NULL,
hall_info_id integer REFERENCES hall_information(id),
hall_type character varying(20)
);
and my query to fetch all records is
public function get_hall_info(){
$this->db->select('*');
$this->db->from('hall_info');
$this->db->join('hall_pricing', 'hall_info.id = hall_pricing.hall_info_id');
$query =$this->db->get();
return $query->result();
}
I guess this will do my work but i dont have idea to convert my data into this below form.
var hallinfo =
[
{
"hall_name":"Sai mantap","id":"12","service_provider_id":"35",
"slots":[
{
"slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
"hall_info_id":"45","hall_type":"mantap"
},
{
"slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"45","hall_type":"mini"
},
{
"slot_name":"full_day","slot_charge":"15000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"45","hall_type":"bunquet"
}
]
},
{
"hall_name":"Raman mantap ","id":"13","service_provider_id":"36",
"slots":[
{
"slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
"hall_info_id":"46","hall_type":"mantap"
},
{
"slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"46","hall_type":"mini"
},
{
"slot_name":"full_day","slot_charge":"25000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
"hall_info_id":"46","hall_type":"bunquet"
}
]
}
];

Related

Querying on mysql json array using mysql workbench

Here is my json data:
{
"TransactionId": "1",
"PersonApplicant": [
{
"PersonalId": "1005",
"ApplicantPhone": [
{
"PhoneType": "LANDLINE",
"PhoneNumber": "8085063644",
"IsPrimaryPhone": true
}
]
},
{
"PersonalId": "1006",
"ApplicantPhone": [
{
"PhoneType": "LANDLINE",
"PhoneNumber": "9643645364",
"IsPrimaryPhone": true
},
{
"PhoneType": "HOME",
"PhoneNumber": "987654321",
"IsPrimaryPhone": false
}
]
}
]
}
I want to get phone no of the people who have phonetype as landline.
How to do that?
I tried this approach:
#find phoneNumber when phoneType='LANDLINE'
SELECT
#path_to_name := json_unquote(json_search(applicationData, 'one', 'LANDLINE')) AS path_to_name,
#path_to_parent := trim(TRAILING '.PhoneType' from #path_to_name) AS path_to_parent,
#event_object := json_extract(applicationData, #path_to_parent) as event_object,
json_unquote(json_extract(#event_object, '$.PhoneNumber')) as PhoneNumber
FROM application;
The issue with this is that I am using 'one' so I am able to achieve results but here in my json I have 2 people who have type as landline.
Using json search I am getting array of values and I am not able to decide how to extract these array row values in a manner where I can extract paths.
SELECT
#path_to_name := json_unquote(json_search(applicationData, 'all', 'LANDLINE')) from application;
result:
as you can see at 3rd and 4th row i am getting 2 data as an array.
How do I store this data to get the appropriate result?
I also tried one more query but not able to retrieve results for array of data.
I cannot use stored procedure and I have to use mysql workbench.
Please note that I am fresher so I don't know how I can approach this solution for more complex queries where I may have to retrieve id of a person having type as landline (multiple people in single array).
SELECT test.id, jsontable.*
FROM test
CROSS JOIN JSON_TABLE(test.data,
'$.PersonApplicant[*]'
COLUMNS ( PersonalId INT PATH '$.PersonalId',
PhoneType VARCHAR(255) PATH '$.ApplicantPhone[0].PhoneType',
PhoneNumber VARCHAR(255) PATH '$.ApplicantPhone[0].PhoneNumber')) jsontable
WHERE jsontable.PhoneType = 'LANDLINE';
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4089207ccfba5068a48e06b52865e759

How will we ignore duplicates while insert json data into mongoDB based on multiple condition

[
{
"RollNo":1,
"name":"John",
"age":20,
"Hobby":"Music",
"Date":"9/05/2018"
"sal":5000
},
{
"RollNo":2,
"name":"Ravi",
"age":25,
"Hobby":"TV",
"Date":"9/05/2018"
"sal":5000
},
{
"RollNo":3,
"name":"Devi",
"age":30,
"Hobby":"cooking",
"Date":"9/04/2018"
"sal":5000
}
]
Above is the JSON file i need to insert into a MongoDB. Similar JSON data is already in my mongoDB collection named 'Tests'.I have to ignore the records which is already
in the mongoDB based on a certain condition.
[RollNo in mongoDB == RollNo in the json need to insert && Hobby in mongoDB ==Hobby in the json need to insert && Date in mongoDB == Date in the json need to insert].
If this condition matches, i need to igore the insertion,else need to insert the data into DB .
I am using nodejs. Anyone please help me to do it.
If you are using mongoose then use upsert.
db.people.update(
{ RollNo: 1 },
{
"RollNo":1,
"name":"John",
"age":20,
"Hobby":"Music",
"Date":"9/05/2018"
"sal":5000
},
{ upsert: true }
)
But to avoid inserting the same document more than once, only use upsert: true if the query field is uniquely indexed.
The easiest and safest way to do this is by using a compound index.
You can create a compound index like this:
db.people.createIndex( { "RollNo": 1, "Hobby": 1, "Date" : 1 }, { unique: true } )
Then the duplicated inserts will produce an error which you need to process in your code.

Extracting multiple associative objects from JSON type in MySQL

Trying to figure out the best way to query a MySQL table containing a json column.
I am successfully able to get product OR port.
SELECT ip, JSON_EXTRACT(json_data, '$.data[*].product' ) FROM `network`
This will return:
["ftp","ssh"]
What I'm looking to get is something like this, or some other way to represent association and handle null values:
[["ftp",21],["ssh",22],[NULL,23]]
Sample JSON
{
"key1":"Value",
"key2":"Value",
"key3":"Value",
"data": [
{
"product":"ftp",
"port":"21"
},
{
"product":"ssh",
"port":"22"
},
{
"port":"23"
}
]
}

DynamoDB&NodeJS: search table from JSON array

I have a DynamoDB table with only two columns "EmailId" and "SubscriptionId". "EmailId" is Primary sort key and "SubscriptionId" is Primary partition key. I have to insert a record into it but before that I need to make sure that the record does not exist. I get the records from a third party API endpoint in JSON array format. So, I will have to search in the table and the records that do not exist in the will have to be inserted.
The records I get are in the following format. This is a sample response and I can get maybe 1000 of records in the array.
[{
"emailId": "abc1#abc1.com",
"subscriptionId": "A1"
}, {
"emailId": "abc2#abc2.com",
"subscriptionId": "A2"
}, {
"emailId": "abc3#abc3.com",
"subscriptionId": "A3"
}]
I don't want to pick each record from the array above, search the table and if not found, insert it because this table is going to get huge. Is there any other way we can do that? I am using this with NodeJS. Though I can not change the JSON array but I can make changes to DynamoDB table. Any suggestions?
The batchWrite item API can be used to put multiple items in a batch. The maximum number of requests in the batch is 25.
The best part is that if the key in the PutRequest is already present in the table, it updates the item rather than throwing some error or exception (i.e. key is not unique).
The disadvantage of this approach is that the latest update will overwrite all the attributes of the existing item in the table. For example, if the existing item in the table has 5 attributes and the latest update has only 3 attributes, the table will have only 3 attributes (as present in the latest PutRequest) after the latest batch execution.
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
RequestItems: {
"subscription": [
{
PutRequest: {
Item: {
"emailId": "abc1#abc1.com",
"subscriptionId": "A1"
}
}
},
{
PutRequest: {
Item: {
"emailId": "abc2#abc2.com",
"subscriptionId": "A2"
}
}
},
{
PutRequest: {
Item: {
"emailId": "abc3#abc3.com",
"subscriptionId": "A3"
}
}
}
]
}
};
docClient.batchWrite(params, function (err, data) {
if (err) {
console.error("Unable to write item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("Write Item succeeded:", JSON.stringify(data, null, 2));
}
});

Parse JSON object in postgresql (Migrating from MongoDB to PSQL)

I've been using pgfutter to import a JSON file to postgresql.
Now I am trying to parse that json file to my psql table. This is my table:
CREATE TABLE organisation (
id int8 PRIMARY KEY NOT NULL,
department VARCHAR(255),
business_type VARCHAR(255),
unit_active INTEGER
);
This is an example of my json:
{
"extension":[
{
"name":"unitActive",
"value":"Active"
},
{
"name" : "businessType",
"value" : "Other"
},
{
"name":"specialities",
"value":[
{
"value":"allm kirurgi"
},
{
"value":"neurologi"
},
{
"value":"ortopedisk kirurgi"
}]
}],
"catalog":"main",
"department":"hospital"
}
I've been looking at the postgresql documentation but i'm not quite sure how to normalize the object, in this case the extension object in my JSON.
This is what I got
INSERT INTO organisation
SELECT nextval('organisationsSequence'),
data->>'department' as department,
"Something here" as business_type,
"Something here" as unit_active
FROM import.collection WHERE data->>'catalog' = 'main';
So what i'm trying to achieve is to do something like "Select extension.value where extension.name = businessType"