How to access list element in FEEL list literal expression in DMN?

2.8k Views Asked by At

I have the following list of the object:

"animals":[
{
  "family":"cat",
  "color":"grey"
},
{
  "family":"dog",
   "color":"white"
}
]

I want to access first animal object that is in dog family and white color. I am trying to achieve it by doing this:

animals[family = "dog" and color = "white"][0]

But it shows warning as follows:

FEEL WARN while evaluating literal expression 'animals[ family = .... [string clipped after 50 chars, total length is 82]': Index out of bound: list of 1 elements, index 0; will evaluate as FEEL null

What exactly is incorrect here? I feel I am doing wrong something semantically. I also referred FEEL's specification but am unable to figure out what's wrong. I also referred dmn decision modeling documentation for DMN from Redhat but still I am clueless. Please help.

1

There are 1 best solutions below

0
On BEST ANSWER

In FEEL, the list's elements index starts at 1.

So the expression you want to access first animal object, actually is:

animals[family = "dog" and color = "white"][1]

This is documented in the DMN specification at page 126:

The first element of a list L can be accessed using L[1] and the last element can be accessed using L[-1].

DMN specification list index starts at 1

To provide a more friendly reference, this is also documented in Drools documentation

Elements in a list can be accessed by index, where the first element is 1. Negative indexes can access elements starting from the end of the list so that -1 is the last element.

Drools documentation DMN FEEL list index starts at 1

...and equivalently for the productized Red Hat documentation version as well:

Red Hat documentation DMN FEEL list index starts at 1