title: Postgresql, jsonb query help is needed
body: <p>I am very, very embarrassed. But I need a help with my query, thanks in advance.</p>
  
  <p>My jsonb (jsonb array) is </p>
  
  <pre><code> [  
  
     {  
        "id_contact":2,
        "contact_data":{  
           "NM_EMAIL":[  
              "234rtrt@oao.com.ru"
           ],
           "NM_PHONE":[  
              "849533574",
              "849533d575"
           ]
        },
        "resp_ls_data":[  
           "14",
           "11"
        ],
         "pr_from_head":true
     },
     {  
        "id_contact":8,
        "contact_data":{  
           "NM_EMAIL":[  
              "somfdf11m@a.il"
           ],
           "NM_PHONE":[  
              "89234511"
           ]
        },
        "resp_ls_data":[  
           null
        ],
         "pr_from_head":false
     }
  
  ]
  </code></pre>
  
  <p>I figured out the query as </p>
  
  <pre><code>select case when pr_from_head then jsonb_build_object('id_head_cont',id_contact)::text
          when not pr_from_head then id_contact::text
          when id_contact is null then NULL end as est_contact_id,
     contact_data,
     nullif(resp_ls_data, '[null]') resp_ls_array
    from jsonb_to_recordset('
    [  
  
     {  
        "id_contact":2,
        "contact_data":{  
           "NM_EMAIL":[  
              "234rtrt@oao.com.ru"
           ],
           "NM_PHONE":[  
              "849533574",
              "849533d575"
           ]
        },
        "resp_ls_data":[  
           "14",
           "11"
        ],
         "pr_from_head":true
     },
     {  
        "id_contact":8,
        "contact_data":{  
           "NM_EMAIL":[  
              "somfdf11m@a.il"
           ],
           "NM_PHONE":[  
              "89234511"
           ]
        },
        "resp_ls_data":[  
           null
        ],
         "pr_from_head":false
     }
  
  ]
  ') as ls(id_contact integer, contact_data jsonb, resp_ls_data jsonb,pr_from_head boolean) 
  </code></pre>
  
  <p>Got the result as :</p>
  
  <pre><code>"est_contact_id"       ||   "contact_data"    || resp_ls_array"
  "{""id_head_cont"": 2}"||   "{""NM_EMAIL"":.. || "[""14"", ""11""]"
  "8"                    ||   "{""NM_EMAIL"":.."|| NULL
  </code></pre>
  
  <p>But the result i want is :</p>
  
  <pre><code> "est_contact_id"       ||  "contact_data"    || 'resp_ls_array"
    "{""id_head_cont"": 2}"|| "{""NM_EMAIL"":.. || "14"
    "{""id_head_cont"": 2}"|| "{""NM_EMAIL"":.. || "11"
    "8"                    || "{""NM_EMAIL"":.."|| NULL
  </code></pre>
  
  <p>If resp_ls_array is not null I want to separate is to parts.
  Any help would be appreciated.</p>