Check for existence of key in nested roAssociativeArray

This should be simple, but I can’t seem to wrap my head around it :slightly_smiling_face:

I have an associative array called X, and it has an associative array called Y. Y might have a string called Z.
All is fine if Z does exist, but if it doesn’t these methods all result in a crash:

If I try

if x.y.z <> invalid

I get “Syntax Error”

if x.y.lookup("z") <> invalid

I get “Member function not found in BrightScript Component or interface.”

if x.y.DoesExist("z")

I get “Member function not found in BrightScript Component or interface.”

Can anybody point me in the right direction? Thank you!

“gabek” wrote:

if x.y.lookup("z") <> invalid

I get “Member function not found in BrightScript Component or interface.”

if x.y.DoesExist("z")

I get “Member function not found in BrightScript Component or interface.”

Those would seem to indicate that x.y is not a roAssociativeArray. You can check the type by doing something like this…

? type(x.y)
BrightScript Debugger> x = {y: {z: "zzz"} }
BrightScript Debugger> ? x.y.z
zzz
BrightScript Debugger> ? x.y.w
invalid
BrightScript Debugger> ? x.y.doesExist("w")
false

So here’s what it was! Because x.y was an empty array, it’s not seen as an associative array, therefore you can’t query for existence of keys. It all came down to it’s type coming from json when it’s empty. Thanks for pointing me in that direction!

No, it should work even if AA is empty and parseJSON seems to correctly parse empty AA?

BrightScript Debugger> q = chr(34) : j = "{" + q + "y" + q + ": {}}" : ? j
{"y": {}}
BrightScript Debugger> x = parseJSON(j)
BrightScript Debugger> ? type(x.y)
roAssociativeArray
BrightScript Debugger> ? x.y.z 
invalid