Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
gabek
Level 7

Check for existence of key in nested roAssociativeArray

This should be simple, but I can't seem to wrap my head around it Smiley Happy

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!
0 Kudos
4 Replies
Roku Employee
Roku Employee

Re: Check for existence of key in nested roAssociativeArray

"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)
0 Kudos
EnTerr
Level 8

Re: Check for existence of key in nested roAssociativeArray

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
0 Kudos
gabek
Level 7

Re: Check for existence of key in nested roAssociativeArray

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!
0 Kudos
EnTerr
Level 8

Re: Check for existence of key in nested roAssociativeArray

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
0 Kudos