Postgres JSONB - Flatten nested objects and groupings As i've learned, if i need to flatten a JSON contained in a specific field of my postgres db table, i can use one of the following functions/operators
Now, those functions are perfect for a simple for some simple flattening like this one:
{
   &quot;manufacturer&quot;:[
      {
         &quot;manufacturer&quot;:&quot;Tesla&quot;,
         &quot;address&quot;:&quot;Deer Creek Road Palo Alto&quot;,
         &quot;contact&quot;:&quot;support@tesla.com&quot;
      },
      {
         &quot;manufacturer&quot;:&quot;BMW&quot;,
         &quot;address&quot;:&quot;Petuelring 130, 80809 München&quot;,
         &quot;contact&quot;:&quot;support@bmw.com&quot;
      }
   ]
}

Using the jsonb_to_recordset function the json will be flattened without any problem.
Now, here comes my effective question, i'm not able to address in order to solve this problem effectively...
Suppose that every manufacturer has a nested JSON object that exposes the model and starting price like reported in the following JSON
{
   &quot;cars&quot;:[
      {
         &quot;manufacturer&quot;:&quot;Tesla&quot;,
         &quot;address&quot;:&quot;Deer Creek Road Palo Alto&quot;,
         &quot;contact&quot;:&quot;support@tesla.com&quot;,
         &quot;models&quot;:[
            {
               &quot;model&quot;:&quot;Model S&quot;,
               &quot;starting_price&quot;:50000
            },
            {
               &quot;model&quot;:&quot;Model 3&quot;,
               &quot;starting_price&quot;:35000
            },
            {
               &quot;model&quot;:&quot;Model X&quot;,
               &quot;starting_price&quot;:70000
            }
         ]
      },
      {
         &quot;manufacturer&quot;:&quot;BMW&quot;,
         &quot;address&quot;:&quot;Petuelring 130, 80809 München&quot;,
         &quot;contact&quot;:&quot;support@bmw.com&quot;,
         &quot;models&quot;:[
            {
               &quot;model&quot;:&quot;X1&quot;,
               &quot;starting_price&quot;:35000
            },
            {
               &quot;model&quot;:&quot;X2&quot;,
               &quot;starting_price&quot;:32000
            },
            {
               &quot;model&quot;:&quot;X3&quot;,
               &quot;starting_price&quot;:39000
            },
            {
               &quot;model&quot;:&quot;Series 5&quot;,
               &quot;starting_price&quot;:55000
            },
            {
               &quot;model&quot;:&quot;Series 3&quot;,
               &quot;starting_price&quot;:50000
            }
         ]
      }
   ]
}

How i can create a query that also flattens the models object effectively and outputs many rows as the manufacturer models reported in the models object?

Bonus Question:
In addition to that, it's possible to create a query that outputs something like this in a more compact way grouping rows as shown below?

Any help is appreciated!
You need to unnest twice:
select x.m -&gt;&gt; 'manufacturer' as manufacturer,
       x.m -&gt;&gt; 'address' as address,
       x.m -&gt;&gt; 'contact' as contact,
       m.model -&gt;&gt; 'model' as model,
       (m.model -&gt;&gt; 'starting_price')::int as starting_price
from the_table t
  cross join jsonb_array_elements(t.models -&gt; 'cars') as x(m)
  cross join jsonb_array_elements(x.m -&gt; 'models') as m(model)

Suppressing repeating values is better done when you display the data in your application, not in SQL.
Online example