How to get only single sub record form list of dictionary?

Hi Team,

I have list of dict. like:

{{{customername: "Hardy", name: "S1", region: "Asia"}}, 
{{customername: "Len",name: "SRD", region: "NA"}},
{{customername: "MAdusa", name: "Line 9", region: "Australia"}}}

Now I want to store only name values in a local variable.

I need result like, '

local!variable1 = { "S1","SRD","Line 9" }

how to do this with loop ?

Because list of dict. can be more or less or empty so need code which can handle if the list comes empty(can return null), I will be using this code in expression rule

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    The simplest way that works in most cases is to just access the dot property, i.e. "local!myList.customerName", which is NOT null-safe; the null-safe alternative is the Property() function - i.e. "property(local!myList, "customerName", {})", which returns an empty set as a default if the list is empty or no such property is found.

    a!localVariables(
      local!list: {
        {
          customername: "Hardy",
          name: "S1",
          region: "Asia"
        },
        {
          customername: "Len",
          name: "SRD",
          region: "NA"
        },
        {
          customername: "MAdusa",
          name: "Line 9",
          region: "Australia"
        }
      },
      
      local!emptyList: {},
      
      "Customer Names (populated list): " & property(local!list, "customername", {}) 
      & char(10) & char(10)
      & "Customer Names (empty list): " & property(local!emptyList, "customername", {})
      
    )

  • I tried but it works on list of dict. but not on my type i.e

  • 0
    Certified Lead Developer
    in reply to arshbirs0001

    In this case it still works for me:

    However the problem with a list of arbitrary dictionaries, as opposed to an array of the same dictionary, is if there are different properties in the different dictionaries, the system will start behaving weirdly in my experience.  But as long as there's a property with a name matching exactly what you pass into the property() function, it should be resilient enough to work.  Note in this case the property name is case sensitive.

    In this example I've toyed with the property names but the property() function still produces the expected results (as long as we observe the case sensitivity caveat).

Reply
  • 0
    Certified Lead Developer
    in reply to arshbirs0001

    In this case it still works for me:

    However the problem with a list of arbitrary dictionaries, as opposed to an array of the same dictionary, is if there are different properties in the different dictionaries, the system will start behaving weirdly in my experience.  But as long as there's a property with a name matching exactly what you pass into the property() function, it should be resilient enough to work.  Note in this case the property name is case sensitive.

    In this example I've toyed with the property names but the property() function still produces the expected results (as long as we observe the case sensitivity caveat).

Children