Validating JSON causes Crash

I am writing a desktop application that drives a 3D Printer server (Octoprint). I use httpsocket to send a receive json data. I have a thread running that is poling for status information. Unfortunately my app is crashing hard when I run a particular sequence. I believe it’s the structure of the JSON status response. I have tried many things but can’t seem to find a way to validate the JSON coming in. If I disregard the json coming in the app doesn’t crash. So I am pretty sure it’s the json. I captured the json in different instances to compare but that yielded nothing

I have seen and tried many of the examples on the blog etc with no luck. Does anybody have any advice on how to tackle this?

I had this in my code and even the debugger stops on this even if I don’t have a breakpoint. The try doesn’t work or can’t handle the error.

Try Dim x As Auto = Xojo.Data.ParseJSON( src ) MsgLine("Valid JSON") Return True Catch JSONException MsgLine("BAD JSON") Return False End Try

In going to multiple depths of debugging I was able to zero in on the problem. The answer is simple but does not explain the crashes. I need a key value that was three levels deep. To be sure I was seeing all to be seen I wrote a script to hammer the server with status request and dumped whatever came back to a file.

What I found is that some responses did not come back with complete data. There were keys missing but the json was valid. I would have expected to get key not found exception but that did not happen. To solve this I wrote code to navigate down each level to be sure the keys I need were there before trying to process the data.

Is there an easier or better way to check for keys in lower in the tree without navigating down?

A couple of things…

First, this line is wrong, or, at least, doesn’t do what you think it does:

Catch JSONException

That should be something like:

Catch err As JSONException

Otherwise, what you are catching is any exception into a variable named “JSONException”.

Second, if you prefer to deal with the classic framework Dictionary/Variant array, check out my JSONItem_MTC project here. It has functions to parse JSON and generate JSON using the classic framework.


Thank for the correction that helps, I am going to go back and see if that was contributing to my crashes. I have used the classic framework ( JSONItem ) on a previous project. My understanding is that will likely go away in the near future so I am trying to work through the new framework. I have used some of your examples in the past and worked out great.

I doubt JSONItem is going away in our lifetime, but regardless, my code is outside of JSONItem and my JSONItem_MTC.

The string obtained by HTTP is best to make a mandatory type conversion, otherwise it is likely to cause errors.