Can I create a variable name from another variable

ok, Not sure how to word this, but this is what I have / trying to do:

I have read in my JSON items… one of the items contains a set of keys that have more data. I don’t know ahead of time the name of the keys, so I have read the names of the keys into a string array… this works. Then I want to create a new json item based on the names now contained in the string array.

I keep getting an out of bounds error with the following code:

Var jsonBalanceQuarterly as JSONItem=jsonBalanceSheet.Lookup("quarterly", New JSONItem)
Var tYears() as string = jsonBalanceQuarterly.Keys
Var pQdate() as JSONItem

var x as integer = 0
for x = 0 to 20
  var tYearstxt as String = tYears(x)
  var tYearstxt as jsonItem  =jsonBalanceQuarterly.Lookup(tYearstxt,New JSONItem)
    
Next X

I had tried using:
var pQdate(x) in place of the tYearstxt first, but that had errors as well. I thought maybe using tYearstxt as replacement would work but that was not successful either.

pQdate() has the values of:
2023-12-31
2023-09-30
2023-06-30
and on until it has 153 entries in the array, so I want to create json items named with the first 20 entries in the pQdate() array.

I recommend not using Lookup like this. When you do, you create a new JSONItem to pass into the function, even if the key exists. So it would immediately get discarded. There are two ways to avoid this. The first is to pass nil and check for that. It’s more lines of code, of course:

Var jsonBalanceQuarterly As JSONItem = jsonBalanceSheet.Lookup("quarterly", Nil)
If jsonBalanceQuarterly Is Nil Then
  jsonBalanceQuarterly = New JSONItem
End If

Alternatively, you can use the If() function, which looks like it would have the same problem, but does not. It’s just rather… verbose.

Var jsonBalanceQuarterly As JSONItem = If(jsonBalanceSheet.HasKey("quarterly"), JSONItem(jsonBalanceSheet.Value("quarterly").ObjectValue), New JSONItem)

To actually answer your question, the OutOfBoundsException fires access tYears(x), correct? That would mean you do not have the 21 items in the array you expect to have. Try this instead:

For Each tYear As String In tYears
  Var jYear As JSONItem = jsonBalanceQuarterly.Lookup(tYear, Nil)
  // Do more stuff
Next

Thanks Thom… I can work that!