Another Nested Dictionaries Question - Dictionary in a Dictionary in a Dictionary

Why does casting work for a Dictionary in a Dictionary as @Tim_Parnell explains in https://forum.xojo.com/t/nested-dictionaries/44144 but when I try to duplicate the syntax that works for second level Dictionary for a third level Dictionary inside the second level Dictionary in a Dictionary in a Dictionary, autocomplete gives me the third “Value” but the Debugger says I have a Syntax Error. What am I missing? I’ve tried a number of variations and what is shown below is the only one that Autocomplete provides an assist but it won’t compile.

If Not (actn.Value("workstep") = Nil) Then
  Var actnWorkstep As String = Dictionary(actn.Value("workstep")).Value("name") // Works and retirves the value
  Var actnJobID As String = Dictionary((actn.Value("workstep")).Value("job")).Value("id")// Debugger gives a syntax error
  Var actnJobName As String = Dictionary((actn.Value("workstep")).Value("job")).Value("name")// Debugger gives a syntax error
End

The first just works because it get’s what it’s expecting the others did not get what they expected.

If Not (actn.Value("workstep") = Nil) Then
  Var actnWorkstep As String = Dictionary(actn.Value("workstep")).Value("name") // Works and retirves the value, because you cast to Variant and expect a string.
  Var actnJobID As String = Dictionary(Dictionary(actn.Value("workstep")).Value("job")).Value("id") // you have to cast both values to the expected one. 

// Adapt your coding to make it clear what's happening:
Var rootDict As Dictionary = actn.Value("workstep") // Or cast Dictionary(actn.Value("workstep")) 
Var subDict As Dictionary = rootDict.value("job") // Or cast Dictionary(rootDict.value("job"))
actnJobID  = subDict.value("id") // Check the debugger, you'll instantly see all 3 values (rootDict, subDict and actnJobID) and as a bonus it's more readable. 
 
// Try doing the same with this line:
  Var actnJobName As String = Dictionary((actn.Value("workstep")).Value("job")).Value("name")// Debugger gives a syntax error

End
1 Like

I like the solution and it works and looks much cleaner. I decided to declare the string variable as empty first since they would not get declared if the Dictionaries were Nil. Here was my interim test. Ultimately I’ll drop all the string variables and the values will go straight into an array. and the code will look even cleaner.

Var actnWorkstep As String = ""
Var actnJobID As String = ""
Var actnJobName As String = ""
If Not (actn.Value("workstep") = Nil) Then
  Var actnWrkstp As Dictionary = actn.Value("workstep")
  actnWorkstep = actnWrkstp.Value("name") 
  If Not (actnWrkstp.Value("job") = Nil) Then
    Var actnJob As Dictionary = actnWrkstp.Value("job")
    actnJobID = actnJob.Value("id")
    actnJobName = actnJob.Value("name")
  End If
End

Thanks so much DerkJ!

P.S. I still want to know why casting to a third level Dictionary caused a syntax error

1 Like

Side note:

Var actnWorkstep As String = ""
// Is the same as:
Var actnWorkstep As String // As string is not nil it's always "" or "with something inside"

No problem,

The 3th level didn’t work probably because you had a unexpected method call:

// Let's break it up:
//  Dictionary((actn.Value("workstep")) // Cast to Dictionary instance
//  .Value("job")). // <- this is a Variant, a Variant has no .Value("") method... ;)
//  Value("id") // will convert to String 

// You could try this one:
Var actnJobID As String = Dictionary(Dictionary(actn.Value("workstep")).Value("job")).Value("id").StringValue
// All value() return Variants, so it has to be cast to use it as a Dictionary, here above we double case to get the resulting string
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.