JSONItem Clarifications and help

Hello guys,

I have some coding to do to put a web hook for Meta for my tests and it seems that I’m having some trouble with JSONItem and I wanted to know what would be the best way to handle this and if there is a faster and safer way on doing this .

This is the link and I need to get from each JSON payload the

“type”: “text”

In order to process each payload based on its format.

Now in order to get this I tried using payload.HasKey("") but it seems that this does not work and it does not scan trough the JSON, then I tried using payload.Lookup("entry", "") and it does not always works specially when the result is an array I need to do something like scan each part of it , check if this is an array then loop trough that array, see the depth of it, get the needed data and then parse again.

All this needs to be done as well while safe as the JSON that I get is not always same format and I need to know what I’m looking for in order to avoid issues .

Now for my side I did created a class , then I created an exact parsing method for each type of message and try to paste the payload for each in order to get It processed, so my question is , what would be the fastest and safest way to get the

type

value without writing a ton of code and avoiding crashing the app.

Thanks again .

It depends a lot on the structure of your json text. If `“type”: “text”‘ is at the top level, then all you need to do is this:

Var value as String = payload.lookup("type", "")

// or

Var value as String = payload.value("type")

If you’ve got an array, you can do this:

Var arrayValue as JSONItem = payload.Child("arrayObject")
Var c as integer = arrayValue.count
For I as integer = 0 to c-1
  Var child as JSONItem = arrayValue.childAt(I)
  // do something with the child
Next I

My point is that JSONItems behave just like Xojo dictionaries and arrays when it some to parsing them, but it’s expected that you’re going to know the structure of the incoming data. If not, you can certainly write an app to search a json structure.

1 Like

Thanks Greg,

I was considering .lookup as a global search function same as the name says it . Now that you say that lookup works only to top level then that means that if ever the structure changes on the JSON then all will crash.

I assume that I would need to detect the JSON Structure then scan it all over and then get what I need in case found .

I’ll have to go that path I guess .

Thanks again