rakesh
rakesh

Reputation: 41

Iteration over json array elements in PostgreSQL

I've got two JSON data row from the column in postgresql database and that looks like this.

{
  "details":[{"to":"0:00:00","from":"00:00:12"}]
}

{
  "details":[ 
             {"to":"13:01:11","from":"13:00:12"}, 
             {"to":"00:00:12","from":"13:02:11"}
            ]
}

I want to iterate over details and get only the "from" key values using a query in postgresql. I want it like

                             from
                           00:00:12
                           13:00:12
                           13:02:11

Upvotes: 0

Views: 4092

Answers (1)

Kaushik Nayak
Kaushik Nayak

Reputation: 31676

Use jsonb_array_elements

select j->>'from' as "from" from t 
cross join jsonb_array_elements(s->'details') as j;

Demo

Upvotes: 4

Related Questions