I am trying to loops through a json response in CFML, and need to do one call to get the number of pages, then a call for each successive page, then loop through items to get my data. In doing so I have a nested loop that requires a nested variable. So, for example, my initial loop will result in:
#jsonarray.items.1.id#
#jsonarray.items.2.id#
#jsonarray.items.3.id#
#jsonarray.items.4.id#
and so on.
So in my inside loop, I am trying to replace that number by doing another loop like this:
<cfloop from="1" to="50" index="thisBatchItem">
</cfloop>
But then, I would have to nest the index inside my variable, and that of course does not work.
Can someone point to what I am missing here?
Here is the code I am working with, and you will see the obvious places where I have the problem. I put in #jsonarray.items.#thisBatchItem#.id# just to show where I am trying to make this happen. I know it does not work.
<cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=1&per-page=50" throwonerror="false">
<cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
<cfhttpparam type="header" name="Content-type" value="application/json">
</cfhttp>
<cfoutput>
<cfloop from="1" to="#pageCount#" index="thisPage">
<cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=#thisPage#&per-page=50" throwonerror="false">
<cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
<cfhttpparam type="header" name="Content-type" value="application/json">
</cfhttp>
<cfset jsonarray = deserializeJson(httpResp.fileContent)>
<cfloop from="1" to="50" index="thisBatchItem">
<cfif StructKeyExists(jsonarray,"#jsonarray.items.#thisBatchItem#.id#")>
#jsonarray.items.[#thisBatchItem#].id#<br>
</cfif>
</cfloop>
</cfloop>
</cfoutput>