Using a variable when pulling data from JSON array


#1

1) Give a description of the problem
I’m making a request for a JSON. It changes every minute and the record I want is always at the end. I can’t get a variable to work when writing the expression.

2) What is the expected behaviour?
I’m taking the length of the array to get the last value:
set variable lastRecord = $response.length
from there, I’m just trying to get the last record, but it’s not coming though.
$response[lastRecord]

3) What is happening/not happening?
$response[lastRecord] is retuning NULL
$response[160] returns the value, but I need the latest as it refreshes every minute

**4) Post a Green Snapshot of the piston!

5) Attach logs after turning logging level to Full
4/17/2020, 9:31:27 AM +449ms
+1ms ╔Received event [In meeting].switch = on with a delay of 223ms
+79ms ║RunTime Analysis CS > 19ms > PS > 41ms > PE > 18ms > CE
+81ms ║Runtime (41157 bytes) successfully initialized in 41ms (v0.3.110.20191009) (79ms)
+82ms ║╔Execution stage started
+91ms ║║Comparison (enum) on is (string) on = true (1ms)
+93ms ║║Cancelling condition #2’s schedules…
+94ms ║║Condition #2 evaluated true (7ms)
+96ms ║║Cancelling condition #1’s schedules…
+97ms ║║Condition group #1 evaluated true (state changed) (9ms)
+99ms ║║Cancelling statement #3’s schedules…
+107ms ║║Sending external web request to: cloud.iexapis.com/stable/stock/dal/intraday-prices?token=*********************************************
+719ms ║║Executed virtual command httpRequest (613ms)
+725ms ║║Executed virtual command setVariable (3ms)
+730ms ║║181
+731ms ║║Executed virtual command log (1ms)
+737ms ║║null
+738ms ║║Executed virtual command log (1ms)
+744ms ║║[average:24.085, close:24.085, date:2020-04-17, high:24.085, label:12:10 PM, low:24.085, minute:12:10, notional:2553.01, numberOfTrades:2, open:24.085, volume:106]
+745ms ║║Executed virtual command log (1ms)
+747ms ║╚Execution stage complete. (665ms)
+748ms ╚Event processed successfully (748ms)


#2

Without seeing what $response variable contains… try logging the value of $response.lastRecord


#3

That just comes back NULL, as there is no object in the array called lastRecord.
the lastRecord variable is a number for looking at a specific object in the array (the last one to be exact).

This is what the JSON returns:

[{
    "date": "2020-04-17",
    "minute": "09:30",
    "label": "09:30 AM",
    "high": 24.2,
    "low": 23.97,
    "open": 24.135,
    "close": 23.97,
    "average": 24.136,
    "volume": 27936,
    "notional": 674260.32,
    "numberOfTrades": 74
}, {
    "date": "2020-04-17",
    "minute": "09:31",
    "label": "09:31 AM",
    "high": 23.9,
    "low": 23.785,
    "open": 23.9,
    "close": 23.785,
    "average": 23.847,
    "volume": 6830,
    "notional": 162873.21,
    "numberOfTrades": 34
}, {
    "date": "2020-04-17",
    "minute": "09:32",
    "label": "09:32 AM",
    "high": 23.77,
    "low": 23.62,
    "open": 23.77,
    "close": 23.685,
    "average": 23.694,
    "volume": 4532,
    "notional": 107381.155,
    "numberOfTrades": 39
}, {...

Does what I’m trying to do make sense?
I’m just trying to get the last record, but that one is constantly being appended to the dataset.


#4

This gives you the number of records but they are numbered 0-(length-1). Try subtracting 1 and look at that record.


#5

Can you log $response and show us what it contains?


#6

So simple, this did it!

Thank you!


#7

Good catch, @guxdude!