IllegalCastException accessing parsed JSON

Good afternoon (o;

I have a JSON returned from one of my servers which looks like:

{
    "payments": [
        {
            "x": "2022-03-09",
            "y": "1234.56"
        },
        {
            "x": "2022-03-10",
            "y": "1234.56"
        },
    ],
    "pickups": [
        {
            "x": "2022-03-09",
            "y": "1234.56"
        },
        {
            "x": "2022-03-10",
            "y": "1234.56"
        },
    ]
}

So I use to access the first array “payments”:

Var d as dictionary = ParseJSON(textData)
Var days as dictionary = d.Value("payments")

Where the last line throws the illegal cast exception error when running.
Though not sure why as I followed this snippet from the docs page:

// jsonData contains the actual JSON data:
// {"team":"Red Sox","topplayer":{"name":"David Ortiz", "position":"DH", "uniform number":34}}

Var d As Dictionary
d = ParseJSON(jsonData)
Var topPlayer As Dictionary = d.Value("topplayer")

thanks in advance
richard

Dictionary.value is a variant. You need to check if your value is a dictionary and then do a cast.

Figured it out already (o;

ElseIf URL = URLDaily Then
  Var d as dictionary = ParseJSON(textData)
  Var days() As Variant = d.Value("payments")
  payments.RemoveAll
  For each day as Dictionary in days
    Var revenue as String = day.Value("y")
    payments.Add(revenue.ToDouble)
  Next
  DailyCanvas.Refresh
End if

Just need now a nice way to draw fancy bars on the phones screen (o;

I find JSONItem far superior and easier to use. JSONItem children that are JSON Items are also JSONItem class.

var jsItem as new JSONItem(textData)
var jsPayments as JSONItem = jsItem.Value("payments")

for i as Integer = 0 to jsPayments.Count - 1
  var jsThisItem as JSONItem = jsPayments.ValueAt(i)
  // jsThisItem is your x/y item

next i

The global methods ParseJSON and GenerateJSON will only cause you pain in my experience.

Well they already caused me pain compared to using JSON on PHP/Python :wink:

Thanks for the hint :slight_smile: