I am sharing sample data . I am trying to create sql query which runs in postgres database to generate following Json structure .
p_id is my primary key and I want to create array of email address asone array and address and rel type as another array for same person .
select '1' as p_id ,'purnima' as first_name,1234 pp_code,'primary' rel_type,'37/26' addr_1,'[email protected]' email_add,'work' email_type union all
select '1' as p_id ,'purnima' as first_name,6789 pp_code,
'other' rel_cd,'99/44' addr_1,'[email protected]' email_add,'other' email_type union all
select '2' as p_id ,'bhanu' as first_name,3333 pp_code,
'primary' rel_type,'37/26' addr_1,'[email protected]' email_add, 'work' email_type union all
select '2' as p_id ,'bhanu' as first_name,4444 pp_code,
'other' rel_cd,'99/44' addr_1,'[email protected]' email_add,'other222' email_type
Json Output required :
[
{
"p_id": 1,
"first_name": "purnima",
"email_details": [
{
"email_address": "[email protected]",
"email_type": "work"
},
{
"email_address": "[email protected]",
"email_type": "other"
}
],
"personAddresses": [
{
"pp_code": 1234,
"rel_type": "primary",
"addr1": "37/26"
},
{
"pp_code": 6789,
"rel_type": "other",
"addr1": "99/44"
}
]
},
{
"p_id": 2,
"first_name": "bhanu",
"email_details": [
{
"email_address": "[email protected]",
"email_type": "work"
},
{
"email_address": "[email protected]",
"email_type": "other222"
}
],
"personAddresses": [
{
"pp_code": 3333,
"rel_type": "primary",
"addr1": "37/26"
},
{
"pp_code": 4444,
"rel_type": "other",
"addr1": "99/44"
}
]
}
]