Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
gabek
Visitor

Check for existence of key in nested roAssociativeArray

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

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 4
RokuChris
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
Roku Guru

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
Visitor

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
Roku Guru

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
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.