POSTGRESQL extract keys from jsonb - postgresql

I have a table with a jsonb column that contains a lot of external data and I need to extract some keys, but I'm having a hard time doing it.
The table is like that:
code title external_data
G20540 Data Analysis
The external data is the jsonb column, organized with a dict inside an array and it has these infos:
[
{
"DESCR": "Requisito RJ_GRD_GCSOSRJ",
"ORDERNO": "10",
"ACAD_PLAN": "",
"ACAD_PROG": "",
"DESCR254A": "Requisito RJ_GRD_GCSOSRJ_Plan. de Comunicação I",
"DESCRSHORT": "Requisito",
"EFF_STATUS": "A",
"RQ_CONNECT": "",
"ACAD_CAREER": "",
"INSTITUTION": "X",
"PARENTHESIS": "",
"SAA_DESCR80": "Requisito RJ_GRD_GCSOSRJ_Plan. de Comunicação I",
"RQRMNT_GROUP": "000312",
"ACAD_SUB_PLAN": "",
"CREATION_DATE": "2020-04-13T21:26:51.923",
"RQRMNT_USEAGE": "ENR",
"CONDITION_CODE": "CRS",
"CONDITION_DATA": "003130",
"REQUISITE_TYPE": "PRE",
"CONDITION_DESCR": "ID Curso",
"RQRMNT_LIST_SEQ": "1",
"RQ_GRP_LINE_NBR": "0010",
"RQ_LINE_KEY_NBR": "0001",
"RQ_GRP_LINE_TYPE": "CRSE",
"CONDITION_OPERATOR": "EQ"
}
]
I need to extract "DESCR", "ORDENO", "DESCR254A", "SAA_DESCR80", "RQRMNT_GROUP", "RQRMNT_USEAGE", "EFF_STATUS".
I tried with this query but I get only null results for the external_data columns:
SELECT codes.external_id as "code"
,codes.title as "title"
,requirements.external_data ->> 'RQRMNT_GROUP' as "RQRMNT_GROUP"
,requirements.external_data ->> 'EFF_STATUS' as "EFF_STATUS"
,requirements.external_data ->> 'RQRMNT_USEAGE' as "RQRMNT_USEAGE"
,requirements.external_data ->> 'DESCR' as "DESCR"
,requirements.external_data ->> 'SAA_DESCR80' as "SAA_DESCR80"
,requirements.external_data ->> 'DESCR254A' as "DESCR254A"
,requirements.external_data ->> 'ORDERNO' as "ORDERNO"
FROM requirements
LEFT JOIN codes ON codes.id = requirements.code_id
How can I do that?

You'll need to use a WITH clause to identify the table in the array and select from that:
WITH external_codes AS (
SELECT r.code_id,
jsonb_array_elements(r.external_data) AS external_data
FROM requirements r
)
SELECT codes.external_id as "code"
,codes.title as "title"
,external_codes.external_data ->> 'RQRMNT_GROUP' as "RQRMNT_GROUP"
,external_codes.external_data ->> 'EFF_STATUS' as "EFF_STATUS"
,external_codes.external_data ->> 'RQRMNT_USEAGE' as "RQRMNT_USEAGE"
,external_codes.external_data ->> 'DESCR' as "DESCR"
,external_codes.external_data ->> 'SAA_DESCR80' as "SAA_DESCR80"
,external_codes.external_data ->> 'DESCR254A' as "DESCR254A"
,external_codes.external_data ->> 'ORDERNO' as "ORDERNO"
FROM external_codes
LEFT JOIN codes ON codes.id = external_codes.code_id

Related

How to query json objects using Postgres JSONB column

I have JSON stored in a jsonb column:
{
"processedResult": {
"orderPayment": {
"paymentType": "VISA"
},
"store": "US"
}
}
What I have tried:
SELECT DISTINCT myData -> 'processedResult' -> 'orderPayment' -> 'paymentType'
FROM mytable
WHERE myData ->> 'processedResult' ->> 'store' = 'US'
The WHERE clause seems to be incorrect.
Desired Output:
VISA
Mastedcard
Postgres Version: PostgreSQL 11.13
You'll want to use
SELECT DISTINCT myData -> 'processedResult' -> 'orderPayment' ->> 'paymentType'
FROM mytable
WHERE myData -> 'processedResult' ->> 'store' = 'US'
Notice that -> returns the selected jsonb value, whereas ->> always returns a postgres text value (or NULL, or an error).

Postgres jsonb: querying multi level

In a Postgres (9+) table there is a column of type jsonb with the following json:
{
"dynamicFields":[
{
"name":"040",
"subfields":[
{
"name":"a",
"value":"abc"
},
{
"name":"a",
"value":"xyz"
}
]
}
]
}
I would like to write a query that return only the rows where the field name equals 040 and subfield a equals xyz.
This is as far as I got, so far:
select e.obj from my_table
cross join lateral jsonb_array_elements(my_column-> 'dynamicFields') as e(obj)
where e.obj ->> 'name' = '040' and e.obj ->> 'subfields' #> '{"name": "a", "value": "xyz"}'::jsonb
How should this query be to achieve this?
e.obj ->> 'subfields' has a text result. You'll want to use e.obj -> 'subfields' that returns the jsonb value where the #> operator works. Also the containment checks needs to have another array as the right hand side, so that it will test whether all values in the right array are contained in the left array - it doesn't work to pass the element object directly.
select e.obj from my_table
cross join lateral jsonb_array_elements(my_column-> 'dynamicFields') as e(obj)
where e.obj ->> 'name' = '040' and e.obj -> 'subfields' #> '[{"name": "a", "value": "xyz"}]'::jsonb
-- ^ ^ ^
(online demo)
As you have an equality condition you can use the "contains" operator directly by providing a JSON value of what you want. There is no need to unnest the arrays.
select *
from my_table
where my_column -> 'dynamicFields' #> '[{"name": "040", "subfields": [{"name":"a", "value": "xyz"}]}]'
Starting with Postgres 12 an alternative is to a SQL/JSON path operator:
select *
from my_table
where my_column #? '$.dynamicFields[*] ? (#.name == "040").subfields[*] ? (#.name == "a" && #.value == "xyz")'

Convert date in jsonb (Postgres)

I have jsonb column data =
"{"history": [{"endDate": "30.06.2015", "classname": "Class A", "startDate": "2010-04-01", "numberAction": "0016", "positionName": "Teacher"},
{"endDate": "31.06.2010", "classname": "Class A", "startDate": "2005-08-10", "numberAction": "0015", "positionName": "Student"},
{"endDate": "2005.08.09", "classname": "Class B", "startDate": "2005-02-21", "numberAction": "0014", "positionName": " Student "}]}"
As you can see, the dates of the "endDate" in the array are not correct. Please tell me how they can be converted to the format YYYY-MM-DD?
My idle attempt:
UPDATE table
SET data = jsonb_set(data, '{endDate}', to_date('{endDate}', 'YYYY-MM-DD'), false)
WHERE id = 'UUID';
Answer =
update table
set data = data - 'history' ||
jsonb_build_object
( 'history'
, ( select jsonb_agg
( case when aa.value ->> 'endDate' like '%.%' then
aa.value - 'endDate' || jsonb_build_object
( 'endDate'
, to_date(aa.value ->> 'endDate','dd.mm.yyyy')
)
else
aa.value
end
)
from jsonb_array_elements(data -> 'history') as aa
)
)
WHERE uuid = 'UUID'
and exists ( select *
from jsonb_array_elements(data -> 'history') as aa
where aa.value ->> 'endDate' like '%.%'
);

How to flatten multiple row values into concatenated string in postgres?

I have a simple set of three tables . 2 "source" tables and a "join" table that allows one to many relationship.
I have this query:
select data.mph1.data ->> 'name' as name, data.mph1.data ->> 'tags' as Tags,
data.mph2.data ->> 'School' as school from data.mph1
join data.mph1tomph2 on data.mph1tomph2.mph1 = data.mph1.id
join data.mph2 on data.mph2.id = data.mph1tomph2.mph2
The output appears as:
Name Tags School
"Steve Jones" "["tag1", "tag2"]" "UMass"
"Steve Jones" "["tag1", "tag2"]" "Harvard"
"Gary Summers" "["java", "postgres", "flutter"]" "Yale"
"Gary Summers" "["java", "postgres", "flutter"]" "Harvard"
"Gary Summers" "["java", "postgres", "flutter"]" "UMass"
What I'm looking for is
Name Tags School
"Steve Jones" "["tag1", "tag2"]" "UMass", "Harvard"
"Gary Summers" "["java", "postgres", "flutter"]" "Yale, Harvard, UMass"
How would I get this result in single query? Possible?
Use the aggregate function string_agg()
select
data.mph1.data ->> 'name' as name,
data.mph1.data ->> 'tags' as tags,
string_agg(data.mph2.data ->> 'School', ', ') as school
from data.mph1
join data.mph1tomph2 on data.mph1tomph2.mph1 = data.mph1.id
join data.mph2 on data.mph2.id = data.mph1tomph2.mph2
group by data.mph1.id -- if id is a primary key
-- group by 1, 2 -- otherwise

org.postgresql.util.PSQLException: ERROR: set-returning functions are not allowed in WHERE

I need a help for the below mentioned detail,
I am using Postgres + Spring-Data-JPA. Moreover, I have used the jsonb data type for storing data.
I am trying to execute a query, but it gives me the following error:
ERROR: set-returning functions are not allowed in WHERE
The cause here is that I have added a jsonb condition in the WHERE clause (kindly refer to the below query for more detail).
Query (I have renamed column name just because of hiding the actual column name):
select distinct
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'firstName' as firstName,
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'lastName' as lastName,
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'country' as country
from
tale1 table10_
left outer join
table2 table21_
on table10_.end_user_id=table21_.end_user_id
left outer join
table3 table32_
on table10_.manufacturer_id=table32_.manufacturer_id
where
table21_.end_user_uuid=(
?
)
and table21_.is_active=true
and table32_.manufacturer_uuid=(
?
)
and table32_.is_active=true
and table10_.is_active=true
and table32_.is_active=true
and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'action' = ('PENDING')
order by
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'firstName',
jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'lastName'
limit ?
The following line in the above query is causing the error:
and jsonb_array_elements(initiated_referral_detail->'listOfAttribue')::jsonb
->> 'action' = ('PENDING')
Can anyone please guide me about how do fetch data from the inner JSON? Especially in my case I have an inner List and a few elements inside.
I recommend a lateral join with jsonb_array_elements for cases like that. Here is an example:
CREATE TABLE tale1 (
id integer PRIMARY KEY,
initiated_referral_detail jsonb NOT NULL
);
INSERT INTO tale1 VALUES
(1, '{
"name": "one",
"listOfAttribue": [
{ "id": 1, "action": "DONE"},
{ "id": 2, "action": "PENDING" },
{ "id": 3, "action": "ACTIVE" }
]
}');
INSERT INTO tale1 VALUES
(2, '{
"name": "two",
"listOfAttribue": [
{ "id": 1, "action": "DONE"},
{ "id": 2, "action": "ACTIVE" }
]
}');
To find all ids where the associated JSON contains an array element with action = PENDING, you can query like this:
SELECT DISTINCT id
FROM tale1 CROSS JOIN LATERAL
jsonb_array_elements(initiated_referral_detail -> 'listOfAttribue') AS attr
WHERE attr ->> 'action' = 'PENDING';