Select to view content in your preferred language

Arcade Label Expression Value Substitutions

1932
9
Jump to solution
10-26-2023 10:45 AM
ZacharyHart
Honored Contributor

I have a numerical attribute field containing integer values and would like to have the label expression display a corresponding value for each integer. I have created a dictionary representing these values.

However, the label expression dialogue indicates an error in line 2: Invalid KeyI cannot see anything wrong with the format of the dictionary based on other examples found within the forums.

The Arcade expression is shown below.

var stuff = {
  0: "SCC",
  101: "SEP",
  4: "CLT",
  23: "CGS",
  8: "CTR",
  7: "CTH",
  14: "DSW",
  9: "ECT",
  5: "FTH",
  21: "GPS",
  10: "GSL",
  3: "ITH",
  22: "ITS",
  13: "ISH",
  20: "OSR",
  17: "PTC",
  26: "PCT",
  24: "SAL",
  18: "STC",
  15: "SWR",
  19: "STR",
  6: "TFA",
  12: "3SS",
  11: "2SS",
  100: "GPE",
  25: "WHI",
  102: "RET",
  103: "TSI",
  104: "POL"
};

var keyField = "Type"; //this is the attribute field with the integer values
var lookupKey = $feature[keyField];
var lookupResult = Dictionary(stuff)[lookupKey];

0 Kudos
2 Solutions

Accepted Solutions
KenBuja
MVP Esteemed Contributor

A key in the dictionary must be a string, not a number. You'll have to convert your field value to a string, in addition. Here's an example that works.

var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS"
};

var test = Text(0);
var lookupResult = Dictionary(stuff)[test]

 

View solution in original post

ElisabethFenn
Occasional Contributor

Not sure why an arcade expression would work in AGO and not in Pro, but I've come up with a different way to write this that is working in Pro:

var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS",
  "8": "CTR",
  "7": "CTH",
  "14": "DSW",
  "9": "ECT",
  "5": "FTH",
  "21": "GPS",
  "10": "GSL",
  "3": "ITH",
  "22": "ITS",
  "13": "ISH",
  "20": "OSR",
  "17": "PTC",
  "26": "PCT",
  "24": "SAL",
  "18": "STC",
  "15": "SWR",
  "19": "STR",
  "6": "TFA",
  "12": "3SS",
  "11": "2SS",
  "100": "GPE",
  "25": "WHI",
  "102": "RET",
  "103": "TSI",
  "104": "POL"
};

return stuff[Text($feature.Type)]

  

View solution in original post

9 Replies
KenBuja
MVP Esteemed Contributor

A key in the dictionary must be a string, not a number. You'll have to convert your field value to a string, in addition. Here's an example that works.

var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS"
};

var test = Text(0);
var lookupResult = Dictionary(stuff)[test]

 

ZacharyHart
Honored Contributor

I should have mentioned I tried that as well but when doing this I'm given an error of field not found. I know the format for Arcade attribute labels is $feature.fieldname 

var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS",
  "8": "CTR",
  "7": "CTH",
  "14": "DSW",
  "9": "ECT",
  "5": "FTH",
  "21": "GPS",
  "10": "GSL",
  "3": "ITH",
  "22": "ITS",
  "13": "ISH",
  "20": "OSR",
  "17": "PTC",
  "26": "PCT",
  "24": "SAL",
  "18": "STC",
  "15": "SWR",
  "19": "STR",
  "6": "TFA",
  "12": "3SS",
  "11": "2SS",
  "100": "GPE",
  "25": "WHI",
  "102": "RET",
  "103": "TSI",
  "104": "POL"
};

var keyField = "Type"; 

// Look up the value based on the key from the field
var lookupKey = $feature[keyField];
var lookupResult = Dictionary(stuff)[lookupKey];
return lookUpResult;
0 Kudos
KenBuja
MVP Esteemed Contributor

Here's an example using the sample dataset in the Playground (with the first key changed to "1" to get a result)

// Returns the first feature in the layer
var feat = First(
  // Fetches features from a public portal item
  FeatureSetByPortalItem(
    Portal("https://www.arcgis.com"),
    // portal item id
    "7b1fb95ab77f40bf8aa09c8b59045449",
    0, // layer id
    ["*"], // fields to include
    false // include or exclude geometry
  )
);

var stuff = {
  "1": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS"
};

var keyField = 'HasData'
var lookupKey = Text(feat[keyField]);

return Dictionary(stuff)[lookupKey]

 

playgroudn.png

KenBuja
MVP Esteemed Contributor

Also note that I modified the first response to include that you also have to convert the numeric attribute to a string to return a value in the Dictionary.

ZacharyHart
Honored Contributor

I noticed that and i'm still getting the same error about field not found. Perhaps this is an issue with the label expression builder? I'm trying to fetch attributes with that interface. (ArcGIS Pro 3.1.3)

0 Kudos
KenBuja
MVP Esteemed Contributor

You have to use the Expects function at the top of the script when using a variable for the field.

Expects($feature, "Type")
var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS"
};
var keyField = "Type"; 

// Look up the value based on the key from the field
var lookupKey = Text($feature[keyField]);
var lookupResult = Dictionary(stuff)[lookupKey];
return lookUpResult;

 

ZacharyHart
Honored Contributor

Ok new clues. I'm going to mark your other response as the solution since it got us to where we need to be, but the rest looks like we have found a potential bug. 

A colleague tested for me and the expression works just fine in AGO. For the heck of it, I saved a copy of that layer in AGO with the working label expression and brought it into Pro. Labeling failed to work. Checked the expression and same error about field not found.


I tried to add Expects($feature, "Type")

and that returns a new error of invalid JSON. So everything works fine in AGO but there's a problem with this solution in Pro.

Thanks for everything! 

0 Kudos
ElisabethFenn
Occasional Contributor

Not sure why an arcade expression would work in AGO and not in Pro, but I've come up with a different way to write this that is working in Pro:

var stuff = {
  "0": "SCC",
  "101": "SEP",
  "4": "CLT",
  "23": "CGS",
  "8": "CTR",
  "7": "CTH",
  "14": "DSW",
  "9": "ECT",
  "5": "FTH",
  "21": "GPS",
  "10": "GSL",
  "3": "ITH",
  "22": "ITS",
  "13": "ISH",
  "20": "OSR",
  "17": "PTC",
  "26": "PCT",
  "24": "SAL",
  "18": "STC",
  "15": "SWR",
  "19": "STR",
  "6": "TFA",
  "12": "3SS",
  "11": "2SS",
  "100": "GPE",
  "25": "WHI",
  "102": "RET",
  "103": "TSI",
  "104": "POL"
};

return stuff[Text($feature.Type)]

  

ZacharyHart
Honored Contributor

Thanks for this Elisabeth!

I'm curious about the disparity between AGO in Pro in the implementation of Arcade regarding dictionaries now.

0 Kudos