SELECT d.id,
OBJECT v.name:v.val FOR v IN d1 END.*
FROM default AS d
LET d1 = ARRAY_FLATTEN(ARRAY (ARRAY {"name":IFMISSINGORNULL(TO_STR(v.type),"")||f1, "val":v1} FOR f1:v1 IN v WHEN f1 != "type" END) FOR v IN d.arr1 END,1);
WITH obj AS ( {"phone":"Phone", "email":"EmailAddress", "PRIMARY":"primary", "SECONDARY":"secondary"})
SELECT d.id,
OBJECT v.name:v.val FOR v IN d1 END.*
FROM docs AS d
LET d1 = ARRAY_FLATTEN(ARRAY (ARRAY {"name":IFMISSINGORNULL(obj.[v.type],"")||obj.[f1], "val":v1} FOR f1:v1 IN v WHEN f1 != "type" END) FOR v IN d.arr1 END,1);
{
"id": "0123",
"primaryEmailAddress": "cb@gmail.com",
"primaryPhone": "9999999999",
"secondaryEmailAddress": "cb_1@gmail.com",
"secondaryPhone": "9999988888"
}