Fetching data from a REST call out of JSON result

1
0
-1

I'm still learning many basic steps in Bonita, so sorry if this is a stupid question ...

In order to do some automation in processes, I call the REST API of another application, which returns a JSON encoded string with three values, one or two of which I'd like to store in a BPOject in Bonita for further use.

I have the connector set up correctly to do the REST call, with a some sample data for now.

I assume to feed in some of the BPOject data I have gathered in a set of data, I will need to define a process variable first, create the JSON string which I put into that variable, then use the "{{someVariable}}" syntax in the body (or I guess I probably could just use the 2-3 input variables inside the body as part of the complete JSON string). This part I guess I have covered.

Problem is reading the information I get back. I found one post here that speaks of how to get the data back, by adding the process variable name to one of the five result fields on the REST definition. Anyway, I need some help with plucking the JSON result string apart and reading the desired values, as well as storing them in the BP Object ... how do I define the script, and how does the script need to look? The JSON string is something like this:

{"TicketID":"375275","TicketNumber":"10213414","ArticleID":"725602"}

I need at least the TicketNumber and possibly the TicketID field read and stored ...

Help appreciated!

Comments

Submitted by gkgba_1406595 on Tue, 05/19/2020 - 19:37

I did some tests (outside of Bonita), something like this might work to take the result apart:

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def reslt = '{"TicketID":"375275","TicketNumber":"10213414","ArticleID":"725602"}'

def object=jsonSlurper.parseText(reslt)
println(object.TicketID)
println(object.TicketNumber)

Missing some basics though in order to get the input from the REST call, and putting the two results back into the Bonita object/database ...

1 answer

1
0
-1

The rest connector already have an output call bodyAsObject that is the result of the jsonSlurper.parseText(json)

You have to store the desired attribute in a variable using the left operand of the connector output (either use a process variable or a business variable), use the proper operator for the operation (either a setter if the left operand is an object or takes values if it is a primitive type). Then the right operand can be a groovy expression: bodyAsObject.TicketID

HTH

Romain

Notifications