Using a variable after $response


#1

1) Give a description of the problem
I have created a static JSON file to store data locally, and I plan on using webCoRE to pull that data. I am having no problem accessing or parsing that data, but for some reason, I cannot seem to use a variable in my response line. I must have tried 15 different variations.

Works:

$response.2019[0]

Fails:

define integer year = 2019
$response.{year}[0]

I would be grateful if anyone can point me in the right direction.

In the screenshot, the last two variables populate perfectly, but “esDays” never does if I try to use a variable. Hopefully, I just have the wrong syntax.


GET request: how to read variables with periods in the name?
#2

Try escaping {}


#3

Thanks for the suggestion, but I tested 5 variations of escape characters, and all have failed…

Grrr… This sounds so simple.


#4

LOL, just throwing stuff out at you to see which ones you haven’t tried yet…

What about setting the variable and concatenating as an expression… $response + year + .[0].eqspr_dayofyear


#5

That was one I hadn’t tried yet… Unfortunately, it failed both with and without the curly brackets around year.

I have even searched the forums for “$response.” and could not find a single example of a variable there.
(although the search does not search the text in green snapshots, so I am still optimistic)


#6

Here is an example with $json; it should work the same with $response[year][0]. What you cannot do is include expressions inside the indexing brackets - just individual variables or literal text. In other words, if you wanted to access $response[year - 1][0] you would need to store the expression year - 1 to a variable like lastYear and access the value with $response[lastYear][0]


#7

Thanks for the suggestions, @ipaterson. Unfortunately, I could not get either method to work:
$response[year][0] .
$response.[year][0]

The only one that works is without a variable:
$response.2019[0] .


The first “Set variable” below is still broken, but the second one works.

temp

What am I doing wrong?


#8

Hrm I can’t check in depth right now but maybe try the year as a string rather than an integer?


#9

I played with it earlier also, making the year a string didn’t help.

Edit: As I was going to stop messing with it for the day… this one worked.

image

image

image


#10

Hot-diggity! Let me edit my JSON to remove the year array so I can test without the [0].


#11

I apologize for the delayed response…
(the rabbit hole went deep with this one)

When I edited the JSON to remove the year array, then I was successful using a variable with $response.

temp

(the names have all changed due to all the edits on my end, but the concept remains)