0

I have the following record in the table table_json:

Table:

  id        doc
  ---------------------------------------
  1 
            {"name":"Shaw",
                 "address":{"line1":"L1",
                            "line2":"L2",
                            "zipcode":"12345"
                           }
            }

Note: Column doc is of type json. Now i want to print the json data into the form of the following one.

Expected output:

  id    name    address
  --------------------------
   1    Shaw    L1,L2,12345
1

1 Answer 1

3

Use json_each_text() in lateral join:

with a_table (id, doc) as (
values
(1, '{
    "name": "Shaw", 
    "address":{
        "line1":"L1",
        "line2":"L2",
        "zipcode":"12345"
        }
    }'::json)
)
select 
    id, 
    doc->>'name' as name, 
    string_agg(value, ',') as address
from a_table, 
lateral json_each_text(doc->'address')
group by 1, 2;

 id | name |   address   
----+------+-------------
  1 | Shaw | L1,L2,12345
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

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.