Acorel
Gratis demo

CPI: A basic introduction to the JsonSlurper

Said Ait Haddou Ali

REST APIs are commonly used in any integration landscape, but especially within the SAP CX suite. That’s why I would advise anyone interested in the integration part of SAP to get acquainted with the JsonSlurper as soon as possible. It is a powerful JSON parser that offers lots of flexibility in modifying a JSON body or reading key value pairs. This blog is meant as a first basic introduction to the JsonSlurper. The JsonSlurper can be made available in your groovy scripts by importing the json package (groovy.json.*).

So what is the JsonSlurper? It is a class that parses JSON text into Groovy data structures such as maps, lists and primitive types (String, Integer, Boolean etc.). So a flat JSON string is converted into a map that you can navigate through to pinpoint any specific key/value pairs (usually the primitive types). If the JSON contains a collection, the collection itself is converted to a list.

A couple of simple examples should help you get started quickly. Consider the following JSON body:

json example2

Let’s introduce the Active flag to the json body.  Additionally we remove the email address from the Json body:

groovyscript1

The steps in the groovy function are as follows: the JSON messagebody is retrieved and parsed by the JsonSlurper. The resulting JsonDataObject is modified to change the JSON structure. The email field is removed and the active flag is added. Finally the JSON structure is converted to a string again and added to the message. The resulting message body:

json example3

Notice how the active flag value contains a boolean value and not a string, which would likely be the case if the JSON body would be constructed manually without the JsonSlurper.

Now let us consider changing the JSON body in a more dynamical way. Let’s assume we would like to remove all extension fields from the body. And an extension field can be identified by the postfix ‘_KUT’ ( the C4C connoisseurs will know this stands for Key User Tool 🙂 ) . The following function could be used for that. We already mentioned that the JsonSlurper converts the JSON body into a map through which we can loop or navigate. We can use that to pinpoint the key value pairs to remove:

groovyscript2

After fetching the messagebody the steps are now as follows: we introduce a String list which will contain the keys that need to be removed from the JSON body. And we loop through the key value pairs of the JSON via the each() statement. Keys ending with the relevant postfix are added to the list of keys to be removed. Then we loop through this list to remove the individual keys from the JSON as we did in the previous groovy script. And presto, the extension fields are removed:

json example4

Hope this short introduction to the JsonSlurper helps. P.S.: The JsonSlurper has a cousin called XmlSlurper. The same concept, but for XML messages.

Said Ait Haddou Ali

Read all my blogs

Receive our weekly blog by email?
Subscribe here: