In order to do this, you'll need to convert the groups HashMap into a JSON object before putting it into your attributes HashMap.
Something like:
...
groups.add(groupMap1);
groups.add(groupMap2);
customAttributes.put("Groups", JSONObject(groups));
The syntax for converting the HashMap to a JSONObject will vary depending on which JSON library your project has access to. If it does not have a method built in, then you will have to manually loop through your HashMap in order to convert it into a proper JSONObject.
Edit:
Since the groups object is being passed in as a string, you can use JSON.parse to convert it into a JSON object.
function getSecretData(){
var user = WL.Server.getActiveUser();
var attributes = user.attributes;
var groups = JSON.parse(attributes.Groups);
return {
groups: groups
};
}