I have a JSON that has this flat structure:
[{
"PK": "1111",
"SOURCE_DB": "Oracle",
"CONTACT_TYPE": "Phone",
"CONTACT_SUBTYPE": "Work",
"EMAIL": null
"PHONE_COUNTRY_CODE": "44",
"PHONE_NUMBER": "12345678",
"PHONE_EXT": "907643",
"STATUS": "Active"
}, {
"PK": "1111",
"SOURCE_DB": "Oracle",
"CONTACT_TYPE": "Phone",
"CONTACT_SUBTYPE": "Home",
"EMAIL": null
"PHONE_COUNTRY_CODE": "353",
"PHONE_NUMBER": "87654321",
"PHONE_EXT": null,
"STATUS": "Active"
}, {
"PK": "1111",
"SOURCE_DB": "",
"CONTACT_TYPE": "Email",
"CONTACT_SUBTYPE": "Personal",
"EMAIL": "me@mail.com"
"PHONE_COUNTRY_CODE": null,
"PHONE_NUMBER": null,
"PHONE_EXT": null,
"STATUS": "Active"
},
{
"PK": "2222",
"SOURCE_DB": "DB2",
"CONTACT_TYPE": "Phone",
"CONTACT_SUBTYPE": "Home",
"EMAIL": null
"PHONE_COUNTRY_CODE": "44",
"PHONE_NUMBER": "98761234",
"PHONE_EXT": null,
"STATUS": "Inactive"
}, {
"PK": "2222",
"SOURCE_DB": "DB2",
"CONTACT_TYPE": "Email",
"CONTACT_SUBTYPE": "Work",
"EMAIL": "you@mail.co.uk"
"PHONE_COUNTRY_CODE": null,
"PHONE_NUMBER": null,
"PHONE_EXT": null,
"STATUS": "Active"
}
]
Then, I want to group them, first by Key (PK), then within each entry, ContactMethods will be grouped together. This is the output:
{
"Accounts": [{
"Reference": {
"Key": "1111",
"System": "Oracle"
},
"ContactMethods": {
"Phone": [{
"Subtype": "Work",
"CountryCode": "44",
"Number": "12345678",
"Extension": "907643",
"Active": true
}, {
"Subtype": "Home",
"CountryCode": "353",
"Number": "87654321",
"Extension": null,
"Active": true
}
],
"Email": [{
"Subtype": "Personal",
"EmailAddress": "my@mail.com",
"Active": true
}
]
}
}, {
"Reference": {
"Key": "2222",
"System": "DB2"
},
"ContactMethods": {
"Phone": [{
"Subtype": "Home",
"CountryCode": "44",
"Number": "98761234",
"Extension": null,
"Active": false
}
],
"Email": [{
"Subtype": "Work",
"EmailAddress": "you@mail.co.uk",
"Active": true
}
]
}
}
]
}
I am able to group this by PK, but I am having difficulty on the second part, on how to do the grouping within the nested structure. Can you show a sample spec and put some explanation?