Can I add a field to a mapping via groovy script?

I've had a bit of trouble with scripting as I couldn't find much documentation. My problem is that I need to programmatically add a field that is a list of objects. I found out here that the field will not behave as expected unless it has a nested type. With my groovy script I can set the field to its new value ctx._source.field = [object] but this does not give the field a nested type.

Long question short: Is there a way to add a field with a specific type to a mapping in a groovy script?