Grabbing an array of Dictionaries from a JSON Item

I receive a file from a website as JSON using the file Downloader. I then Load the file into a JSONItem to parse. The issue is, The data I need to parse is an array of Dictionaries, example Below…

{
“apiresponse” : {
“metrics” : [
{
“tsevent” : “2021-05-07 18:49:05”,
“endpoint” : “wEZHC.syncAppointment”,
“subscriber” : “Cottonwood Ophthalmology Assoc”,
“elapsetm” : 78,
“utc” : 1620434945634
},
{
“tsevent” : “2021-05-07 18:48:55”,
“endpoint” : “wEZHC.syncAppointment”,
“subscriber” : “PAIN INSTITUTE OF NEVADA”,
“elapsetm” : 78,
“utc” : 1620434935118
},
{
“tsevent” : “2021-05-07 18:48:25”,
“endpoint” : “wEZHC.syncAppointment”,
“subscriber” : “CITY OF LONGVIEW - TX”,
“elapsetm” : 94,
“utc” : 1620434905224
},

I need to parse the array “metrics”, and deal with each instance (Loading to a graph canvas). So far, I have been able to locate the root key “apiresponse”. But now, I am at a loss as to how to proceed. Any suggestions?

You didn’t show your code, but you want something like this:

var j as new JSONItem( jsonString )
var apiResponse as JSONItem = j.Child( "apiresponse" ) // Edited
var metrics as JSONItem = apiResponse.Child( "metrics" )
for index as integer = 0 to metrics.LastRowIndex
  var item as JSONItem = metrics( index )
  ...
next index

(Untested.)

Here is the code using ParseJSON:

var j as Dictionary = ParseJSON( jsonString )
var apiResponse as Dictionary = j.Value( "apiresponse" )
var metrics() as Variant = apiResponse.Value( "metrics" )
for index as integer = 0 to metrics.LastRowIndex
  var item as Dictionary = metrics( index )
  ...
next index

Thank you Kem! That is exactly the kickstart I needed! I’ll test it out, but I simply couldn’t find an example of how to do this in all of the searching I’ve done so far. All of the examples are pretty basic and simple. I’ll run both of these up the flagpole and see who salutes them.

1 Like

With the ParseJSON Example I got an Illegal Cast exception, JSONItem cannot be cast into dictionary. I’ll check into that issue later.
With your first suggestion, it works a treat! Nice to have two tools in the toolbox, even if one needs a little WD40.

Glad it worked out.

In the second example, JSONItem isn’t involved at all, so I think you might have missed that in your code.