Even though I’ve used Xojo for many years, there are parts of it I have seldom and sometimes have never had a need for. Up until now I’ve never needed to parse JSON and quite honestly I rarely use Dictionaries in most of the apps I create so combining the two has me a bit confused about the proper syntax to get what I want. I’ve been searching through other posts and what I have found either doesn’t work for my case or I lack some basic ability to connect the dots from what I’m reading.
With that said I have anew system that has recently been installed in our manufacturing environment that I need to extract data from using the vendor’s REST API, and the data is returned as JSON. There a lot of options for what to retrieve but what I’m working with right now is a list of press activities during a specific time frame. The retrieving the JSON part is easy, but with all our other systems what I get is XML not JSON and I’m struggling to loop through the data to make it usable in a table I need to display and calculations to graph some of the activity values.
Here is a small sample of the JSON I get back from this system’s REST API:
{“error”:null,“pageInfo”:{“offset”:0,“pageSize”:5},“activities”:[{“id”:null,“name”:null,“timeTypeName”:null,“timeTypeGroupName”:null,“workstep”:null,“device”:{“id”:“114”,“name”:“Heidelberg_1”,“classId”:“ID_SheetfedPress”},“goodCycles”:0,“wasteCycles”:0,“startTime”:“2021-04-06T10:04:41-05:00”,“endTime”:“2021-04-06T10:04:41-05:00”,“comment”:null,“costCenter”:null,“employees”:[{“id”:“50059”,“name”:“Raleigh”,“firstName”:“Jason”}]},{“id”:“4100”,“name”:“Good production”,“timeTypeName”:“Execution time”,“timeTypeGroupName”:“Production time”,“workstep”:{“id”:“_210402_132438407_015664”,“name”:“Sheet_2 4/4”,“job”:{“id”:“030008-21”,“name”:“2021 Jefferson Middle School”}},“device”:{“id”:“114”,“name”:“Heidelberg_1”,“classId”:“ID_SheetfedPress”},“goodCycles”:405,“wasteCycles”:22,“startTime”:“2021-04-06T10:02:01-05:00”,“endTime”:“2021-04-06T10:04:41-05:00”,“comment”:null,“costCenter”:“114”,“employees”:[{“id”:“50059”,“name”:“Raleigh”,“firstName”:“Jason”}]},{“id”:“4000”,“name”:“Basic makeready”,“timeTypeName”:“Setup time”,“timeTypeGroupName”:“Production time”,“workstep”:{“id”:“_210402_132438407_015664”,“name”:“Sheet_2 4/4”,“job”:{“id”:“030008-21”,“name”:“2021 Jefferson Middle School”}},“device”:{“id”:“114”,“name”:“Heidelberg_1”,“classId”:“ID_SheetfedPress”},“goodCycles”:0,“wasteCycles”:45,“startTime”:“2021-04-06T10:00:37-05:00”,“endTime”:“2021-04-06T10:02:01-05:00”,“comment”:null,“costCenter”:“114”,“employees”:[{“id”:“50059”,“name”:“Raleigh”,“firstName”:“Jason”}]},{“id”:null,“name”:null,“timeTypeName”:null,“timeTypeGroupName”:null,“workstep”:null,“device”:{“id”:“114”,“name”:“Heidelberg_1”,“classId”:“ID_SheetfedPress”},“goodCycles”:0,“wasteCycles”:0,“startTime”:“2021-04-06T10:00:36-05:00”,“endTime”:“2021-04-06T10:00:37-05:00”,“comment”:null,“costCenter”:null,“employees”:[{“id”:“50059”,“name”:“Raleigh”,“firstName”:“Jason”}]},{“id”:“4100”,“name”:“Good production”,“timeTypeName”:“Execution time”,“timeTypeGroupName”:“Production time”,“workstep”:{“id”:“_210401_153703613_002173”,“name”:“Sheet_5 4/4”,“job”:{“id”:“042732-21”,“name”:“2021 Eisenhower Middle School”}},“device”:{“id”:“114”,“name”:“Heidelberg_1”,“classId”:“ID_SheetfedPress”},“goodCycles”:0,“wasteCycles”:0,“startTime”:“2021-04-06T10:00:00-05:00”,“endTime”:“2021-04-06T10:00:36-05:00”,“comment”:null,“costCenter”:“114”,“employees”:[{“id”:“50059”,“name”:“Raleigh”,“firstName”:“Jason”}]}]}
I have several tools to make this JSON more readable and I know the top level three values, error which will always be empty when the query is valid, pageinfo which is an array of values and the thing I need to actually use an Array that contains other single values and arrays. I created a property in the test window I’m working with
Public Property jsonDict As Dictionary
so that I can view the contents in the debugger. What I see when when I run this line of code
jsonDict = ParseJSON(jsontext)
and look at the variable in the debugger is that error= nill, pageinfo = Dictionary and activities = Object(4) which is an array of 5 Dictionaries. Here is where I kind of can’t figure out what to do to loop through the array and get the values I need. I was wanting to make it a dictionary of Dictionaries by itself but that fails when I try this:
If jsonDict.HasKey("activities") Then Var pressactivities As Dictionary = jsonDict.Value("activities") End If
Which gives me an illegal cast exception.
So any help on how to approach this? What I want is for each dictionary in Activities, I want to create an array of values in columns pulled from the dictionary and any dictionaries it contains which I’ll display in a listbox. I’m probably losing it because it’s late in the day and might see it differently in the morning, but any advice you can give on how to loop through an array of Dictionaries like in my sample JSON to pull specific values would be immensely helpful.