PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come eseguire query su array nidificati in una colonna json postgres?

Usa jsonb_array_elements() in lateral join tante volte quanto la profondità di un array json quali elementi vuoi confrontare:

select 
    schools->>'school_id' school_id,
    addresses->>'addr_id' addr_id,
    addresses->>'house_description' house_description,
    addresses->>'house_no' house_no
from title_register_data,
jsonb_array_elements(address_data->'schools') schools,
jsonb_array_elements(schools->'addresses') addresses
where addresses->>'house_description' = addresses->>'house_no';

 school_id | addr_id | house_description | house_no 
-----------+---------+-------------------+----------
 1         | 4       | 1                 | 1
(1 row)