JSON TypeMismatchException

I will admit to being a total novice when it comes to JSON and in my first attempt to parse data from a HTTP GET from a production system I have encountered a TypeMismatchException using code from the example project “JSON Example.xojo_binary_project” combined with the “HTTP Example.xojo_binary_project”

I can successfully retrieve the JSON data from the server:

{"linkedResource":[],"jobletID":null,"internalJobID":"f60cb80580b94b1daf8c3912428e8465","id":null,"propertySet":[],"properties":{"planDescriptor_name":"Perfect Bind - Xerox","custom_Signaturename":null,"custom_Final":null,"job_activeWorkCells":"","job_customerOrderId":"29154","job_submittedTime":"2016-12-28T10:32:53-06:00","job_rootJobletId":"452776","custom_Locationid":null,"c_PressTicketID":null,"job_hasLateTask":null,"job_dueDate":"2017-01-20T10:31:00-06:00","job_inputConfiguration":null,"job_customerJobName":null,"job_completedTime":"2016-12-30T07:07:44-06:00","job_rootJobletStatus":"ready for next processor","c_SkipPersonalization":null,"job_batchName":null,"custom_Trimsize":null,"custom_Signatureid":null,"job_nextDropDeadDate":null,"job_priority":"50","planDescriptor_id":"SaPerfectBindCover","job_condition":"OK","job_hasRework":"false","job_customerOrgName":null,"job_orderQuantity":"1","job_status":"Completed","c_CoverType":null,"job_name":"029154-16.CM_DCE.1A.PDF","job_dueDay":"5","job_id":"f60cb80580b94b1daf8c3912428e8465","custom_Booid":null,"job_reworkRequested":"false","job_modifiedTime":"2016-12-30T07:07:44-06:00"},"link":[{"type":null,"desc":null,"rel":"self","href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465","op":null},{"type":null,"desc":null,"rel":"delete","href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465","op":null},{"type":null,"desc":null,"rel":"printableticket","href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465/printableticket/pdf","op":null}],"expand":null}

Using Kem T’s code for “Making pretty JSON” I can process the raw text to look like this:

{ "linkedResource":[], "jobletID":null, "internalJobID":"f60cb80580b94b1daf8c3912428e8465", "id":null, "propertySet":[], "properties":{ "planDescriptor_name":"Perfect Bind - Xerox", "custom_Signaturename":null, "custom_Final":null, "job_activeWorkCells":"", "job_customerOrderId":"29154", "job_submittedTime":"2016-12-28T10:32:53-06:00", "job_rootJobletId":"452776", "custom_Locationid":null, "c_PressTicketID":null, "job_hasLateTask":null, "job_dueDate":"2017-01-20T10:31:00-06:00", "job_inputConfiguration":null, "job_customerJobName":null, "job_completedTime":"2016-12-30T07:07:44-06:00", "job_rootJobletStatus":"ready for next processor", "c_SkipPersonalization":null, "job_batchName":null, "custom_Trimsize":null, "custom_Signatureid":null, "job_nextDropDeadDate":null, "job_priority":"50", "planDescriptor_id":"SaPerfectBindCover", "job_condition":"OK", "job_hasRework":"false", "job_customerOrgName":null, "job_orderQuantity":"1", "job_status":"Completed", "c_CoverType":null, "job_name":"029154-16.CM_DCE.1A.PDF", "job_dueDay":"5", "job_id":"f60cb80580b94b1daf8c3912428e8465", "custom_Booid":null, "job_reworkRequested":"false", "job_modifiedTime":"2016-12-30T07:07:44-06:00" }, "link":[ { "type":null, "desc":null, "rel":"self", "href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465", "op":null }, { "type":null, "desc":null, "rel":"delete", "href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465", "op":null }, { "type":null, "desc":null, "rel":"printableticket", "href":"http://clkswjmhpctr.clkmfg.jostens.com/rest/jobs/f60cb80580b94b1daf8c3912428e8465/printableticket/pdf", "op":null } ], "expand":null }

When I attempt to parse this data regardless of whether I’ve made it pretty I get the TypeMismatchException at the link array. “An exception of class TypeMismatchException was not handled. The Application Must be shut down.”

The code I’m using to parse the JSON is directly from the “JSON Example.xojo_binary_project”

For i As Integer = 0 To obj.Count-1
	If obj.IsArray Then
			JSONList.AddRow(CStr(i))
			JSONList.cell(JSONList.LastIndex, 1) = obj(i)
	Else
			Dim key As String = obj.Name(i)
			If obj.value(key) IsA JSONItem Then
					JSONList.AddFolder(key)
					JSONList.RowTag(JSONList.LastIndex) = obj.Value(key)
					JSONList.Expanded(JSONList.LastIndex) = True
			Else
					JSONList.AddRow(key)
					JSONList.Cell(JSONList.LastIndex, 1) = obj.Value(key)
			End If
	End If
Next i

The exception occurs at the line: JSONList.cell(JSONList.LastIndex, 1) = obj(i)
The why of it is escaping me, not to mention if there is a work around. I’ve attempted to parse the JSON item into a Dictionary with the same results. It seems no matter what I query from this service parsing fails. Anybody have any advice?

.Cell requires a string, but it sounds like obj is an array of objects, if you’re using obj.Name as a string.

I don’t think that is the problem. I have made zero modification from the example code in “JSON Example.xojo_binary_project” shown above. obj is a JSONItem. ShowJSONObject(obj as JSONItem) The method and listbox were directly copied into my test project.

After thinking about it a while longer you are right. The example code appears to not recurse through an array since that wasn’t part of the example JSON text. I guess that means I need to figure out how to do that since the JSON I’m retrieving does use an array. Any thoughts on the best way to go about that would be appreciated.

if JSONItem.IsArray then // explore it and get the values you want end

You need to recursively call ShowJSONObject if the array is an array.

[code]Public Sub ShowJSONObject(obj as JSONItem)
For i As Integer = 0 To obj.Count-1
If obj.IsArray Then
If obj(i) Isa JSONItem Then ’ Need to add a folder and recursively call ourself
JSONList.AddFolder(CStr(i))
ShowJSONObject(obj(i))
Else
JSONList.AddRow(CStr(i))
JSONList.cell(JSONList.LastIndex, 1) = obj(i)
End If
Else
Dim key As String = obj.Name(i)
If obj.value(key) IsA JSONItem Then
JSONList.AddFolder(key)
JSONList.RowTag(JSONList.LastIndex) = obj.Value(key)
JSONList.Expanded(JSONList.LastIndex) = True
Else
JSONList.AddRow(key)
JSONList.Cell(JSONList.LastIndex, 1) = obj.Value(key)
End If
End If
Next i
End Sub

[/code]

This replacement will display your data as expected.

Wayne you are amazingly generous. That works perfectly! Thank you for the code modification.

Note to Xojo: Wayne’s code above should replace the existing code in the example project “JSON Example.xojo_binary_project” in the method ShowJSONObject(obj as JSONItem)

As a novice to JSON parsing I really appreciate the help!

You should have a look at the new framework where the json data is presented as a dictionary that can include dictionaries. I find it much easier to work with.