PostgreSQL – How to Unnest and Display Elements of a JSON Array

jsonpostgresql

i have this table :

enter image description here

And i want display all the names in the table like this :

enter image description here

Also i want to list the number of gender each genre : Desired output:

enter image description here

Best Answer

According to the documentation, the #> operator comes in useful:

-- name and genre of people (1 row per person)
select
    json_array_elements(
      t.test#>'{clients, info}'
        )::json
    ->>'nom' as nom,
    json_array_elements(
      t.test#>'{clients, info}'
        )::json
    ->>'genre' as genre
from my_table t
-- number of people, grouped by gender (1 row per gender)
select
      json_array_elements(
         t.test#>'{clients, info}'
           )::json
           ->>'genre' as genre,
      count (t.*) as count
from my_table t
group by 1

SqlFiddle