I am able query the data that I want from an integration within a process model, but when I try to map the Json results to a CDT process variable, all of the fields are blank. The keys from the Json dictionary are identical to the names for each field in the CDT. How can I map the Json dictionary into the CDT process variable?
Alternatively, how can I create an expression rule to parse each field from the Json and map each of them to a text process variable?
the following expression returns all key, value pairs from the Json: a!fromJson(rule!<rule_name>().result.body)
However, the following returns a null value:
a!fromJson(rule!<rule_name>().result.body).keyname1
Discussion posts and replies are publicly visible
Seems this is occurring due to the way your data from the integration is structured. You might be getting a list of Dictionary as shown in this example.
What is the output when you cast it? Try this:
index(cast('type!yourCDT?list', a!fromJson(...result.body), "keyname1", "No Value")
It says "expected 2 parameters, but found 4 parameters" when including this portion of the expression: , "keyname1", "No Value")
When I use the following ER I get the following error:
index(cast('type!{urn:com:appian:types}apiResultsData'(), a!fromJson(rule!API_Results(null).result.body)))
Expression evaluation error at function 'cast' [line 4]: Could not cast from apiResultsData to Number (Integer). Details: CastInvalidCould not cast from apiResultsData to Number (Integer). Details: CastInvalid
I am not trying to cast to an integer, as there are only text fields in the Json response and in the CDT.
The following is the result from the API Call:
Raw response body:
body: "{"args":{"ID":"99887766","File_Name":"PT_1","Type":"PT","Prob":"0.77"},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-5f19cd4a-e32e53040178811afc5c3291","user-agent":"Appian","accept-encoding":"gzip,deflate"},"url":""">postman-echo.com/get error: null (Null) connectedSystem: null (Connected System)
When I convert JSON to Appian value:
body Dictionary
remove the () after type!{urn:com:appian:types}apiResultsData'() and instead do this:
index(cast('type!{urn:com:appian:types}apiResultsData', a!fromJson(rule!API_Results(null).result.body)))
or if you think you are going to get multiple values then:
index(cast('type!{urn:com:appian:types}apiResultsData?list', a!fromJson(rule!API_Results(null).result.body)))
if I run the first query above, I get the following error:
Expression evaluation error at function 'index': Too few parameters for function; expected at least 2 parameters, but found 1 parameters.
if I run the second query above, the error is: Invalid type for type constructor: apiResultsData?list
index(cast('type!{urn:com:appian:types}apiResultsData', a!fromJson(rule!API_Results(null).result.body)), "No Data")
that query executes but returns a null value when I replace "No Data" with a dictionary key
Based on the raw response posted above your body contains "headers". This doesn't seem right. Headers should not be inside the body - see the structure of https://docs.appian.com/suite/help/20.2/Integration_Object.html#result. Once you get the correct body contents with your data you should be able to index into your fields correctly.
Thanks! That was the issue. The dictionary I wanted was nested as the value for the key "args", so the following query returns what I wanted.
index(cast('type!{urn:com:appian:types}apiResultsData', a!fromJson(rule!API_Results(null).result.body).args))
Can I add a query parameter of args to the Integration to enter this into the CDT, or is it still best to use casting?
Glad that helped! I don't think a query parameter is going to help you in this specific case since you are already getting the desired result in the result+body. Also, you don't need casting, just choose Convert JSON to Appian value For Response Body Parsing in your Integration Object configuration and save the output of the Integration rule object to a local or ri variable in a!save().
Thanks! I'm now able to pass the dictionary through the process model.
However, I am now stuck on trying to pass a list of dictionaries. The ER above that produced the dictionary I wanted now produces a list of dictionaries that share the same keys.
When I use the same expression as before to create a custom node output for the integration in the process model, it only returns one dictionary instead of all dictionaries. When i check the array box of each field in the CDT to allow multiple values to be passed in, I get the following error:
An error occurred while evaluating expression: =pv!APIResults.ID (Invalid index: Cannot index property 'ID' of type Text into null value of type apiResultsData?list) (Data Inputs)
What is the output of a!fromJson(rule!API_Results(null).result.body).args)? Seems like this is resulting in a null value. Once you get the correct value from the body you will be able to index into the ID field, File_Name field, etc.