I’m tryng to use a join function in my SQL request.
I have two table :
tbl_jsontesting
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| id | data | name |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | {"complexProperties":[{"properties":{"key":"Registred","Value":"123456789"}},{"properties":{"key":"Urgency","Value":"Total"}},{"properties":{"key":"ImpactScope","Value":"All"}}]} | Some Text |
| 2 | {"complexProperties":[{"properties":{"key":"Registred","Value":"123456788"}},{"properties":{"key":"Urgency","Value":"Total"}},{"properties":{"key":"ImpactScope","Value":"All"}}]} | Some Text2 |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tbl_registred
----------------------
| id | name |
----------------------
| 123456789 | Source |
| 123456788 | Cars |
----------------------
The id from the table tbl_registred is linked to the id contained in table tbl_jsontesting in the column data when key is set to Registred
My current query :
select (j -> 'properties' ->> 'Value') as "Registred", "registred_name"
from
(
select json_array_elements("data"::json -> 'complexProperties') as j, tbl_registred.name as "registred_name"
from tbl_jsontesting
LEFT JOIN tbl_registred ON tbl_jsontesting.id = tbl_registred.id
) as arrj
where j -> 'properties' ->> 'key' = 'Registred';
Result :
------------------------------
| Registred | registred_name |
------------------------------
| 123456788 | null |
| 123456789 | null |
------------------------------
My expected output :
------------------------------
| Registred | registred_name |
------------------------------
| 123456788 | Cars |
| 123456789 | Source |
------------------------------
My fidle :
https://www.db-fiddle.com/f/5Jvq4SXUpBvJsY7H3G13xm/1
2
Answers
In your data there is not match between
tbl_registred.id
andtbl_jsontesting.id
so your JOIN give no result.According to your data I guess you want to join
tbl_registred.id
withtbl_jsontesting > complexProperties > properties > Value
where the key is ‘Registred’This is a long path to find the desired object so I will decompose the Json
you probably can do shorter but this way work and with the explosion of the json you can do what you want
result in your fiddle :
You can convert the array elements into rows and then join on the
Value
property:Another option is to use a JSON path query to get the
Value
for a specifickey
As there is no direct cast from
jsonb
totext
we must use the somewhat ugly hack#>> '{}'
to convert it to a text value.Alternatively you can convert the
id
column to a JSON scalar in the join condition:Note that
jsontesting.data
should be defined asjsonb
to avoid the annoying and costly cast.