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

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"

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

Import nested JSON data from from API into a single table in SQL Server

I have some nested JSON data that I get from a API URL that I need to store into a single table in SQL Server, but I have not been able to find a solution that does not involve using multiple tables.
What I have, and what I have tried below this:
This is the JSON data I get from API:
{
"Localnumber":"8888",
"Name":"Some Name",
"Description":null,
"Email":"some#email.com",
"PhoneNumbers":[
{
"Number":"88888888",
"LineName":null,
"BelongsTo":"8888",
"GotoLocalNumber":null
},
{
"Number":"22222222",
"LineName":null,
"BelongsTo":"8888",
"GotoLocalNumber":null
}
],
"Phones":[
{
"LocalNumber":"200000",
"Name":null,
"Type":21,
"MAC":null,
"BelongsTo":"8888",
"description":"Myfone"
},
{
"LocalNumber":"200001",
"Name":null,
"Type":8,
"MAC":"22222222",
"BelongsTo":"2931",
"description":"Mobile (22222222)"
},
{
"LocalNumber":"200002",
"Name":null,
"Type":23,
"MAC":null,
"BelongsTo":"8888",
"description":"Some Description"
}
],
"Info":[
{
"Type":0,
"Label":null,
"Value":null
}
],
"Department":null
}
I have tried in the small just to see if I could get some data from it using the below:
DECLARE #JSON varchar(max)
SELECT #JSON = BulkColumn
FROM OPENROWSET (BULK 'https://public.somedomain.net/api/employee?account=XXXX&hash=XXXXXXXXXXXXXXXXXXXXXX', SINGLE_CLOB) import
SELECT *
FROM OPENJSON (#JSON)
WITH
(
[Localnumber] varchar(20),
[Name] varchar(20),
[Description] varchar(20),
[Email] varchar(20)
)
But I get the following error:
Msg 4861, Level 16, State 1, Line 2
Cannot bulk load because the file "https://public.sippeer.dk/api/employee?account=8493&hash=1SORJvBGzfCMXvc2kwGfbbC-HTIkJd" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
Any help that can just get me "on track" of solving this is highly appreciated.

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"
}
]
}

Give name to an array of Json in JSON.stringify

In PHP when I want to give a name to an array of json objects I use this line.
$json = json_encode(array("users" => $output));
And this variable would be printed like this.
{"users":[{"user_id":"1"}
But now, I'm building a project in Node.js and I need to give a name to an array, and I don't know how to do it, I'm printing data using this line:
res.send(JSON.stringify(users, null, 4));
[ { "user_id": "1" }, { "user_id": "2" } ]
Thank you.
Just build the object with "root" key users before stringify
res.send( JSON.stringify( { 'users': users } , null, 4) );
You can send json directly specifying data points
res.json({ variablename : thevalue (in your case array)})
if more than one value, just follow the syntax
res.json({ variablename : thevalue1, variablename : value2)})
You can something like this:
{'users': JSON.stringify(users, null, 4)}

How can I embed repeated data into single JSON object

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"
}
]
}
];