Confused how to save returned data to BPM or Process Variable using GET Connector

1
0
-1

So, I'm still stuck with my GET Connector. Here is my JSON :

{"_type":"Collection","total":3,"count":3,"pageSize":30,"offset":1,"_embedded":{"elements":[{"_type":"User","id":6,"name":"June Reddington","createdAt":"2022-04-05T02:17:39Z","updatedAt":"2022-11-23T03:27:26Z","login":"june@gmail.com","admin":false,"firstName":"June","lastName":"Reddington","email":"june@gmail.com","avatar":"https://secure.gravatar.com/avatar/1aeb9bb2baefec634371eb480af4ad04?default=404&secure=true","status":"active","identityUrl":null,"language":"en","_links":{"self":{"href":"/api/v3/users/6","title":"June Reddington"},"memberships":{"href":"/api/v3/memberships?filters=%5B%7B%22principal%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%226%22%5D%7D%7D%5D","title":"Members"},"showUser":{"href":"/users/6","type":"text/html"},"updateImmediately":{"href":"/api/v3/users/6","title":"Update june@gmail.com","method":"patch"},"lock":{"href":"/api/v3/users/6/lock","title":"Set lock on june@gmail.com","method":"post"}}},{"_type":"User","id":5,"name":"Ryzact Jr","createdAt":"2022-04-05T02:17:15Z","updatedAt":"2022-04-06T01:30:51Z","login":"ryzact@gmail.com","admin":false,"firstName":"Ryzact","lastName":"Jr","email":"ryzact@gmail.com","avatar":"https://secure.gravatar.com/avatar/eb80b2ba217da41ee1ace8227232fe31?default=404&secure=true","status":"active","identityUrl":null,"language":"en","_links":{"self":{"href":"/api/v3/users/5","title":"Ryzact Jr"},"memberships":{"href":"/api/v3/memberships?filters=%5B%7B%22principal%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%225%22%5D%7D%7D%5D","title":"Members"},"showUser":{"href":"/users/5","type":"text/html"},"updateImmediately":{"href":"/api/v3/users/5","title":"Update ryzact@gmail.com","method":"patch"},"lock":{"href":"/api/v3/users/5/lock","title":"Set lock on ryzact@gmail.com","method":"post"}}},{"_type":"User","id":3,"name":"OpenProject Admin","createdAt":"2022-04-05T02:11:22Z","updatedAt":"2023-01-31T09:26:37Z","login":"admin","admin":true,"firstName":"OpenProject","lastName":"Admin","email":"admin@example.net","avatar":"https://secure.gravatar.com/avatar/cb4f282fed12016bd18a879c1f27ff97?default=404&secure=true","status":"active","identityUrl":null,"language":"en","_links":{"self":{"href":"/api/v3/users/3","title":"OpenProject Admin"},"memberships":{"href":"/api/v3/memberships?filters=%5B%7B%22principal%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%223%22%5D%7D%7D%5D","title":"Members"},"showUser":{"href":"/users/3","type":"text/html"},"updateImmediately":{"href":"/api/v3/users/3","title":"Update admin","method":"patch"},"lock":{"href":"/api/v3/users/3/lock","title":"Set lock on admin","method":"post"}}}]},"_links":{"self":{"href":"/api/v3/users?offset=1&pageSize=30"},"jumpTo":{"href":"/api/v3/users?offset=%7Boffset%7D&pageSize=30","templated":true},"changeSize":{"href":"/api/v3/users?offset=1&pageSize=%7Bsize%7D","templated":true}}}

I need to retrieve Name and ID of the users from the nested JSON (bodyAsString._embedded.elements[*all_array*].name, etc). My end goal is to create a SELECT options with all of this data.

I successfully return only from one array but I can't retrieve all of them (dynamically). I tried using process variable and make it multiple, and no luck.

Can someone help me with this? Thanks :(

1 answer

1
0
-1

Hi,

You can use a Business Object MyUser with name and id string attributes.

The add a multiple business variable in a process.

In the connector output, assign the business variable to the following script:

import com.company.model.MyUser

def body = new JsonSlurper().parseText(bodyAsString)
body._embedded.elements.collect { user ->
   def myUser = new MyUser()
   myUser.name = user.name
   myUser.id = user.id
  return myUser
}

HTH
Romain

Comments

Submitted by UndergradLads on Tue, 02/21/2023 - 11:21

Hi again, I tried your code but it throws some errors :
- myUser is a process variable and should not be used in a declaration statement
- Groovy:unable to resolve class MyUser

import groovy.json.JsonSlurper
def body = new JsonSlurper(bodyAsString)
body._embedded.elements.collect {user ->
  def myUser = new MyUser()
   myUser.name = user.name
   myUser.id = user.id
  return myUser
}

Love to hear from you back again :)

Submitted by romain.bioteau on Tue, 02/21/2023 - 12:07

You must import the type with the proper package.

import com.company.model.MyUser
....
Submitted by UndergradLads on Tue, 02/21/2023 - 14:47

Whoops my bad :(

After I add that import com.company.model.MyUser, I test it and got this error :

java.lang.reflect.InvocationTargetException
org.bonitasoft.engine.bpm.connector.ConnectorExecutionException: USERNAME=install | org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: groovy.json.JsonSlurper(String)

What should I do? Thanks in advance!

Submitted by romain.bioteau on Tue, 02/21/2023 - 16:51

I fixed the code in the answer. Don't hesitate to check the javadoc/groovydoc for those types when you are stuck.

Submitted by UndergradLads on Wed, 02/22/2023 - 04:40

Owhh okayy :)

I use your code and test it using groovy and webservice connector and it threw this error :

java.lang.reflect.InvocationTargetException
org.bonitasoft.engine.exception.BonitaRuntimeException: Unable to deserialize object



com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : com.company.model.MyUser
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /map/entry/list/com.company.model.MyUser
line number : 6
class[1] : java.util.HashMap
required-type[1] : java.util.HashMap
converter-type[1] : com.thoughtworks.xstream.converters.collections.MapConverter
version : 1.4.19
-------------------------------
com.thoughtworks.xstream.mapper.CannotResolveClassException: com.company.model.MyUser

And now what's wrong with it? :(
Thanks in advance!

Notifications