Return Value In Dictionary

I have this dictionary that I want to extract values from depending on one of its values:

a!localVariables(
  local!myDictionary: {
    {
      user : 
      {
          info :{
            { 
              name: {codeValue: 1, nameValue: "engineer"}, type : { codeValue: "role", nameValue: "role" }
            },
            { 
              name: {codeValue: "s101", nameValue: "science"}, type : { codeValue: "dept", nameValue: "dept" }
            }
          }
      }
    },
    {
      user: { {},{} }
    }
  },
  local!tempResults:
  //maybe too much going on
  index(
    if(isnull(property(local!myDictionary.user,"info",null)),"",local!myDictionary.user.info)
    ,if(isnull(property(local!myDictionary.user,"info",null)),"",wherecontains("role",touniformstring(local!myDictionary.user.info.type.nameValue))),""),
  local!tempResults
)

I'm wanting to return either the 'codeValue' or 'nameValue' from 'name' (ie.1,engineer,s101,science), depending if the 'type' has either a 'codeValue/nameValue' of either "role" or "dept". I want both of the object results to return as well, with the second 'user' object just being an empty string (""). 

With this it looks like its just getting the last item out and on the second 'user' object, it is empty and comes back null and the property doesn't exist so it throws an error and why I used the property() function often to cancel the error, but I feel like its too much for maybe something simple. Any suggestions are appreciated.

  Discussion posts and replies are publicly visible

  • 0
    Certified Senior Developer

    Hi

    I am not sure what exactly are you trying to get. But code below may resolve your problem,

    a!localVariables(
      local!myDictionary: {
        {
          user: {
            info: {
              {
                name: { codeValue: 1, nameValue: "engineer" },
                type: { codeValue: "role", nameValue: "role" }
              },
              {
                name: { codeValue: "s101", nameValue: "science" },
                type: { codeValue: "dept", nameValue: "dept" }
              }
            }
          }
        },
        { user: { {}, {} } }
      },
    a!forEach(
      items: local!myDictionary,
      expression: {
        if(
          a!isNullOrEmpty(
            index(
            index(
            index(index(fv!item,"user",{}),"info",{}),
            "type",
            {}
            ),
            "nameValue",
            {}
            )
          ),
          "",
          index(index(fv!item,"user",{}),"info",{})
        )
      }
    )
    )

  • This returns all of them back, which is useful. But what I was trying to do is: if I select either codeValue/nameValue of "role" or "dept" from "type" object, then I get the values from "name" object for either codeValue/nameValue. So if I use a wherecontains("role"), then the value of either of "engineer" or "1" or is returned.

  • 0
    Certified Lead Developer
    in reply to here2learn

    FWIW it'd probably be useful to cut down on some of the whereContains() and property() calls to get a better handle on the data structure and how it's being interacted with by the Appian code. 

    In this example I've gotten rid of the empty second user entry as having an empty set there breaks stuff.  You'll need to do some work here with respect to figuring out the actual data structure before it'll work quite right, as the dictionary/array structure is getting a bit muddled (and i'm having a hard time even discerning what it's supposed to actually be).

    Here I've added a new local variable representing which tag we currently want to search for (just for ease of testing).  Switch the [1] to [2] and vice versa to see the different outputs possible.

    a!localVariables(
      local!currentlySearchingFor: {
        "dept",
        "role"
      }[1], /* change between 1 and 2 to test easily */
      
      local!myDictionary: {
        user: {
          info: {
            {
              name: { codeValue: 1, nameValue: "engineer" },
              type: { codeValue: "role", nameValue: "role" }
            },
            {
              name: { codeValue: "s101", nameValue: "science" },
              type: { codeValue: "dept", nameValue: "dept" }
            }
          }
        }
      },
      
      a!forEach(
        local!myDictionary.user.info,  /* loop over Info entries in the User property */
        if(
          fv!item.type.codeValue = local!currentlySearchingFor,
          fv!item,
          {}
        )
      )
    )

  • 0
    Certified Lead Developer
    in reply to here2learn

    Here's a subsequent example but with the data structure corrected to turn "myDictionary" into an array of dictionaries where each entry represents a single "user" essentially - note that it involves removing the unnecessary and redundant property name of "user:".  It also means now we have to nest a!forEach() calls, once to loop over the existing users, and once to loop over the existing "info" entries for that user.

    a!localVariables(
      local!currentlySearchingFor: {
        "dept",
        "role"
      }[1], /* change between 1 and 2 to test easily */
      
      local!myDictionary: {
        {
          name: "user 1",
          info: {
            {
              name: { codeValue: 1, nameValue: "engineer" },
              type: { codeValue: "role", nameValue: "role" }
            },
            {
              name: { codeValue: "s101", nameValue: "science" },
              type: { codeValue: "dept", nameValue: "dept" }
            }
          }
        },
        {
          name: "user 2",
          info: {
            {
              name: { codeValue: 2, nameValue: "math dude" },
              type: { codeValue: "role", nameValue: "role" }
            },
            {
              name: { codeValue: "s201", nameValue: "math" },
              type: { codeValue: "dept", nameValue: "dept" }
            }
          }
        }
      },
      
      a!forEach(
        local!myDictionary, /* loop over "users" */
        a!forEach(
          fv!item.info,  /* loop over Info entries in the current User */
          if(
            fv!item.type.codeValue = local!currentlySearchingFor,
            fv!item,
            {}
          )
        )
      )
    )