Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
lalit2054_46
Participant

Introduction


It is quite easy to evaluate Xpath expression in Iflow and assign them to Property or Header. But there is no direct way of evaluating JSON body and extract information we require. We can write a Groovy script to parse JSON body and extract information required but then we have to write custom script for each payload type. In this blog I'll try to provide generic Groovy script which can be used to extract values from JSON body.

GPath option in Groovy


The most common way of querying XML in Groovy is using GPath. In order to use GPath for JSON body we need to parse the input body using JsonSlurper. GPath uses dot notation to evaluate, so something very similar to javascript. As an example, you can specify a path to an object or element of interest:
{
"a": {
"b": {
"c": "hello"
}
}
}

a.b.c → yields the c properties for all the b properties of a

More information regarding Gpath can be found here - https://groovy-lang.org/processing-xml.html#_gpath

Evaluate GPath Expression


Now we know GPath expression, but in order to evaluate the expression we will be using groovy.util.Eval class which is available with Groovy. This class is a simple helper on top of GroovyShell. You can use it to evaluate small Groovy scripts that don't need large Binding objects.

Define Parameters


Parameters to Groovy script will be passed via properties -

gpath_expr (Required) - Just like Xpath expression we should have input parameter to pass GPath expression.
gpath_setproperty (Optional) - Extracted value should be assigned to this property
gpath_setheader (Optional) - Extracted value should be assigned to this header

if both gpath_setproperty and gpath_setheader are not defined, the extracted value will be set gpath_result property

Code


import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap

def Message processData(Message message) {
try {
def jsonSlurper = new groovy.json.JsonSlurper();
def properties = message.getProperties();
if (properties.get('gpath_expr') != null) {
def request = jsonSlurper.parseText(message.getBody(java.lang.String) as String);
def gpath_result = groovy.util.Eval.x(request, 'x.' + properties.get('gpath_expr'));
if (properties.get('gpath_setproperty') != null)
message.setProperty(properties.get('gpath_setproperty'), gpath_result.toString());
else if (properties.get('gpath_setheader') != null)
message.setHeader(properties.get('gpath_setheader'), gpath_result.toString());
else
message.setProperty('gpath_result', gpath_result.toString());
}
} catch (Exception ex) {
message.setProperty('gpath_error', ex.getMessage());
}
return message;
}

Example


Let's see below JSON body example -
{
"Attachments": [
{
"Data": "V2VsY29tZXRvbXl3b3JsZA==",
"FileId": "fileid123"
}
],
"EventName": "RequestReceived",
"EventProperties": {
"OrderId": "12345",
"Timestamp": "2022-02-01T11:00:00"
},
"Severity": "INFO",
"TxId": "2345r"
}

Pass gpath_expr as below -

  • gpath_expr = 'EventProperties.OrderId' , gpath_setproperty = 'OrderId'   -> Output property OrderId = '12345'

  • gpath_expr = 'EventProperties.Timestamp' , gpath_setheader = 'CreationTime'   -> Output header CreationTime = '2022-02-01T11:00:00'

  • gpath_expr = 'Attachments[0].FileId'    -> Output property gpath_result = 'fileid123'


 







Summary


In this way you can dynamically use GPath expression to evaluate and extract data from JSON. Kindly comment below and let me know if you more suggestion or ideas.
1 Comment
Labels in this area