Jsonitem.valueat fails

If I don’t missunderstand the docs, this schould work as expected?!

dim data as new jsonitem("{'bla':'foo'}")
Dim key As String = data.KeyAt(0)
Dim value As String = data.ValueAt(0)

But valueAt raises JSONException “JSONItem is not an array”
Of course, I can do “Dim value As String = data.ValueAt(data.keyAt(0))”

What am I missing?

Marius

i believe valueAt i only meat for if .IsArray is true…

are you sure this works?
“Dim value As String = data.ValueAt(data.keyAt(0))”

I believe that won’t work perhaps you mean
“Dim value As String = data.Value(data.keyAt(0))”
may work but i suspect not.

If you really need it by some index instead of name of the key
use the keys() Array and read value from key name.

Sorry, yes. Of course it has to be value not valueAt :wink:

My question is not about a Workaround! (And I can’t hear this word anymore. Xojo is living from workarounds…)

This is what the documentation sais:

image

And - of course I’n native german and perhaps missunderstand that - for me that does not sound like : You get an Item from an array.

did you try to use the variant ?

Dim value As Variant = data.ValueAt(0)

I’m not sure why you expect xojo to behave like this but it’s probably the docs that are wrong.

Do you expect the json to be index-stable when it’s an object? I don’t think that can even be a thing if it’s not an array.

In JSON objects you probably already know the key name in advance. Otherwise just loop trough the keys() Array and extract the values using lookup() or value() the valueAt is meant for arrays as it’s always been.

1 Like

I wasn’t aiming at a workaround. There is never been a object-based valueAt, childAt or keyAt those where always for json arrays … i was trying to see what your use case is.
I guess you expect something that isn’t there… ?

2 Likes

“the JSON Array”.