Data Mapping with Groovy - Part 1

ESB services involve working with a variety of different data formats and structures e.g. XML, JSON, CSV, spread sheets, key-value structures. Transformations between XML and other data structures are quite common when it comes to developing an ESB layer. Mule ESB provides a wide range of choices when it comes to scripting and data transformation.  There’s enough choice to satisfy the proclivities of any developer.

Scripting languages have become popular in recent times because of their first-class support for data structures such as arrays and maps which become a natural complement for markup languages such as XML and JSON. On the JVM, one popular scripting choice is Groovy.

Groovy Primer

The language syntax is mostly similar to Java and it supports calling any existing Java code/libraries. However Groovy’s dynamic typing and optional semi-colons and parentheses make Groovy code more succinct.

For example, compare this simple Java:

With the corresponding Groovy

Groovy also has very clean and powerful syntax for building complex nested data structures (common in data mapping tasks).

For more information on the Groovy language, see the Groovy project home page, this Groovy Syntax Cheat Sheet or this Language Tutorial.

Groovy Builders

Construction of complex hierarchical data structures in Groovy and similar languages uses the ‘builder’ concept. Builders use closures and meta-programming to let developers create the kinds of data structure found in XML, JSON, UI widget hierarchies etc. For example, a basic mapping using a MarkupBuilder:

And a more complex mapping, with namespaces, using a StreamingMarkupBuilder:

Hopefully this post has given you a taste of Groovy and how it can be used for creating data structures and writing data mappings. In Part 2 we’ll cover some approaches and helper code to tidy up Groovy data mapping scripts and make your mappings as clear and concise as possible.


You might also enjoy:

Data Mapping with Groovy - Part 2 02 December 2013

Developing Bulk APIs with Mule, RAML and APIKit 02 December 2014

Dock Tales: Docker Authoring, with Special Guest Mule ESB 30 March 2015

Ansible Crash Course 09 March 2016

Microservices with Apache Camel, Spring Boot and Docker 31 March 2016


comments powered by Disqus