I have following data in my table:
I need to develop a SELECT statement which returns the output as follows:
Following is the script to create table and generate above data:
CREATE TABLE employees.employee_data (
emp_id varchar(50) PRIMARY KEY,
associations JSONB NOT NULL default '{}'::jsonb
);
INSERT INTO employees.employee_data
(emp_id, associations)
VALUES('1001', '{
"group_tags": [
{
"orgId": "XYZCompany",
"locationId": "Location01"
},
{
"orgId": "XYZCompany",
"locationId": "Location02"
}
]
}'::jsonb);
INSERT INTO employees.employee_data
(emp_id, associations)
VALUES('1002', '{
"group_tags": [
{
"orgId": "ABCCompany",
"locationId": "Location03"
},
{
"orgId": "ABCCompany",
"locationId": "Location04"
}
]
}'::jsonb);
Can anyone help me out on this?
2
Answers
Just do a query like this
A version returning text instead of JSON values
Fiddle to test
Just use the function jsonb_array_elements() to get all array elements and extract the content you need:
The operator
->>
returns text from a json element, while->
returns json.