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 !

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    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.

  • I call my expression rule and pass the following data:

    Power=[Unit=KW, Value=88.00],[Unit=HP, Value=120.00]

    from this:

    Engine=[Fuel=[Code=100003, Name=D, NameEx=Diesel], unbounded=, Displacement=[Unit=ccm, Value=1560.00], ValvesPerCylinder=2, CylindersNumber=4, Power=[Unit=KW, Value=88.00],[Unit=HP, Value=120.00]]

    The purpose of my expression rule is to get the value when the unit is equal to "HP"

    The power is expressed in multiple units but I only need one.

    I don't understand why you mention that I have multiple car.

  • +1
    Certified Lead Developer
    in reply to Thomas Loiseau

    The error states there's a vehicleList.  The error explicitly states you're not able to index an array member from an array of CDT.  You have to have 2 arrays at minimum in order to generate the error.

    Perhaps it would be best to look at both how you call it in the rule and how you call it in the process model.

    It might be something as simple as accidentally hitting the check box that says a PV is multiple.  Even if it is only 1 item in that case, it's a 1 length list and won't let you index.

Reply
  • +1
    Certified Lead Developer
    in reply to Thomas Loiseau

    The error states there's a vehicleList.  The error explicitly states you're not able to index an array member from an array of CDT.  You have to have 2 arrays at minimum in order to generate the error.

    Perhaps it would be best to look at both how you call it in the rule and how you call it in the process model.

    It might be something as simple as accidentally hitting the check box that says a PV is multiple.  Even if it is only 1 item in that case, it's a 1 length list and won't let you index.

Children
No Data