Get value from CDT array

I'm facing an issue.

I'm calling an API that returns this response: 

 Engine: Dictionary
    Fuel: Dictionary
        Code: "100003"
        Name: "D"
        NameEx: "Diesel"
    Layout: Dictionary
        Name: "l"
    Power: List of Dictionary: 2 items
        Dictionary
            Value: "88.00"
            Unit: "KW"
        Dictionary
            Value: "120.00"
            Unit: "HP"
    Displacement: Dictionary
        Value: "1560.00"
        Unit: "ccm"
    CylindersNumber: "4"
    ValvesPerCylinder: "2"

I need to get the Power Value when Unit is equal to "HP".

I created this expression rule (and many others):

a!forEach(
  ri!Power,
  if(
    ri!Power[fv!index].Unit = "HP",
    ri!Power[fv!index].Value,
    {}
  )
)

But when I use it inside a process I get this error:

An error occurred while evaluating expression: power:rule!SBX_GetVehiclePowerValue(pv!response.eurotaxIdentificationResponse.VehicleList.Vehicle.Engine.Power) (Expression evaluation error in rule 'sbx_getvehiclepowervalue' at function a!forEach: Cannot index "Power" because it is an array type (List of SBX_Unit_Value_Type). Only fields with scalar types can be indexed from an array.) (Data Outputs)

When I test my expression rule I use this data and it works:

{
  {
    "Value": "88.00",
    "Unit": "KW"
  },
  {
    "Value": "120.00",
    "Unit": "HP"
  }
}

Any lead to resolve this issue ? Thanks !

UPDATE:

Well, it was a problem from the response I get. I had to create a rule to retrieve data from vehicle list

=load(
  a!forEach(
    items: ri!vehiculeList.Vehicle,
    expression: 
      fv!item.Engine.Power
    
  )
)

Thanks for your help !

Parents
  • Looking at your error, it appears to be farther upstream.  It looks like you haven't included the entirety of your response.  If your response has a Vehicle list, and each in a list of Vehicles has an Engine with a list of Power, you've got nested arrays.  You can't index nested arrays all at once.  If you had 10 Vehicles, you'd  be trying to index 10 sets of 2 Powers.  It can do a set of 2, or 10 things, but it can't do 10 sets of 2 all at once.

    The solution is index all the vehicles one at a time in a larger parent rule with a!forEach.  Call a helper rule on each vehicle.  To that rule's perspective, you have only 1 Vehicle (10 times it thinks you only have 1 Vehicle), so you can pull the array of Powers out of just one vehicle.  Then you can get the one "HP" based value and have the helper rule return that, which will return one at a time in the larger a!forEach, resulting in a list of HP values.

Reply
  • Looking at your error, it appears to be farther upstream.  It looks like you haven't included the entirety of your response.  If your response has a Vehicle list, and each in a list of Vehicles has an Engine with a list of Power, you've got nested arrays.  You can't index nested arrays all at once.  If you had 10 Vehicles, you'd  be trying to index 10 sets of 2 Powers.  It can do a set of 2, or 10 things, but it can't do 10 sets of 2 all at once.

    The solution is index all the vehicles one at a time in a larger parent rule with a!forEach.  Call a helper rule on each vehicle.  To that rule's perspective, you have only 1 Vehicle (10 times it thinks you only have 1 Vehicle), so you can pull the array of Powers out of just one vehicle.  Then you can get the one "HP" based value and have the helper rule return that, which will return one at a time in the larger a!forEach, resulting in a list of HP values.

Children

 Discussion posts and replies are publicly visible