PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Wie frage ich verschachtelte Arrays in einer Postgres-JSON-Spalte ab?

Verwenden Sie jsonb_array_elements() in lateral join so oft wie die Tiefe eines json-Arrays, welche Elemente Sie vergleichen möchten:

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)