cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Capturing value from JSON response body !

das_sappo
Participant
0 Likes
3,601

Hi Experts,

Could you please suggest how can I extract the value from 'id' and set it as header in CPI.

I am getting the below response in JSON and need to capture the value of 'id' .

Accepted Solutions (1)

Accepted Solutions (1)

PriyankaChak
SAP Champion
SAP Champion
0 Likes

Hi Avrik,

You can use JsonSlurper.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper;
def Message processData(Message message)
{
        def body = message.getBody(String);
        def map = new JsonSlurper();
        def object = map.parseText(body)
        def id =  object.data.id[0].toString()
        message.setHeader("id",id )
        return message
}
das_sappo
Participant
0 Likes

Thanks Priyanka.

It is working now.

0 Likes

Hi Priyanka,

The above code working perfectly however, I want to fetch the 'id' value starting with "Asset" only.

Suppose there are 4 'id' fields in the json payload and I want to capture the last one (starting with 'Asset' and occurrence is random). Please advise.

"id":"Batch:6166a7e352faff0007dfa7dc",

"id":"assetTypeID:6166a7e352faff0007dfa7dc",

"id":"assetType:6166a7e352faff0007dfa7dc",

"id":"asset:6166a7e352faff0007dfa7dc",

PriyankaChak
SAP Champion
SAP Champion
0 Likes

Try with this.

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

import groovy.json.JsonSlurper

def Message processData(Message message)

{

def body = message.getBody(String)

def map = new JsonSlurper()

def object = map.parseText(body)

object.data.id.each { key ->

if (key.toString().startsWith('asset:'))

message.setHeader("id",key)

}

return message

}

Answers (0)