2

I have this column extra which is JSONB from table called subscribers and the value for a given subscriber is :

{
  "valid": "N",
  "msisdn": "23490272",
  "account_info": [
    {
      "account_id": 110000616,
      "account_cur": "NGN",
      "account_type": "C",
      "account_class": "ww"
    },
    {
      "account_id": 110000617,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "GHCXLA"
    },
    {
      "account_id": 110000618,
      "account_cur": "EUR",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000619,
      "account_cur": "USD",
      "account_type": "C",
      "account_class": "ww"
    },
    {
      "account_id": 110000620,
      "account_cur": "SAR",
      "account_type": "Y",
      "account_class": "ww"
    },
    {
      "account_id": 110000621,
      "account_cur": "NGN",
      "account_type": "Y",
      "account_class": "ww"
    },
    {
      "account_id": 110000622,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000623,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000624,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000625,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000626,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000627,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000628,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000629,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    },
    {
      "account_id": 110000630,
      "account_cur": "NGN",
      "account_type": "S",
      "account_class": "ww"
    }
  ],
  "preapprovalId": "1517299109619",
  "reservedAmount": 0.0,
  "preapprovalStatus": "APPROVED"
}

I want to make the elements from the account_info into columns.I have tried this :

select extra #>'{account_info,0}'->>'account_id' Account,extra #>'{account_info,0}'->>'account_cur' Currency,extra #>'{account_info,0}'->>'account_type' Account_Type from subscribers s2 where id = 319;
  account  | currency | account_type
-----------+----------+--------------
 110000616 | NGN      | C

How can i have all the elements?

1
  • Do you want each array element as a row in your output? Please edit your question (by clicking on the edit link below it) and add the expected output based on your sample data as formatted text. Commented Jul 10, 2020 at 14:17

1 Answer 1

2

If I followed you correctly, you can use jsonb_array_elements() and a lateral join to unnest each array element to a separate row:

select 
    x.acc ->> 'account_id' account,
    x.acc ->> 'account_cur' currency,
    x.acc ->> 'account_type' account_type
from subscribers s
cross join lateral jsonb_array_elements(s.extra -> 'account_info') as x(acc)
where s.id = 319
Sign up to request clarification or add additional context in comments.

1 Comment

GMB thank you that solved my problem. :) !!!!! I wasn't that familiar with lateral joins .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.