mendix / RestServices

REST service module for Mendix. Supports consuming and publishing REST based services and real-time data synchronization. Supports JSON, form-encoded, multipart and binary data transport.
Apache License 2.0
31 stars 46 forks source link

dynamically handle JSON response #50

Closed snuman closed 9 years ago

snuman commented 9 years ago

When calling an external rest service most of the time we receive an array "\"Leg\": " + "[{" + but when the response is a single object we receive an object "\"Leg\": " + "{" + This causes the latest rest module to throw an error java.lang.RuntimeException: org.json.JSONException: JSONObject["Leg"] is not a JSONArray.

We have a reference set called Leg in our domain model.

Is there a workaround to dynamically handle the response?

snuman commented 9 years ago

Here is an article from stackoverflow which describes a possible solution http://stackoverflow.com/questions/14373417/how-to-dynamically-handle-json-response-array-object-using-gson

mweststrate commented 9 years ago

That really sounds like a very bad designed api. Isn't there a parameter that forces an array to be returned so that the response is regular?

For a workaround: you could use get, and don't set a target / stub object (just set it to empty), than get the resonseBody from the response object, check the first character and parse it to the utility json parse functions.

On Tue, Jan 6, 2015 at 10:29 AM, snuman notifications@github.com wrote:

Here is an article from stackoverflow which describes a possible solution

http://stackoverflow.com/questions/14373417/how-to-dynamically-handle-json-response-array-object-using-gson

— Reply to this email directly or view it on GitHub https://github.com/mendix/RestServices/issues/50#issuecomment-68843601.

snuman commented 9 years ago

Response which causes the error { "TripList":{ "noNamespaceSchemaLocation":"http://demo.hafas.de/bin/pub/eurail/rest.exe/v0.8/xsd?name=hafasRestTrip.xsd", "Trip":[{ "LegList":{ "Leg":{ expecting an array here, xsd states 0-12 legs, “Leg”:[{ "idx":"0", "name":"IC 1401", "type":"IC", "reservation":"", "Origin":{ "name":"AMSTERDAM CENTRAAL (Netherlands)", "type":"ST", "id":"8400058", "lon":"4.900829",

Response which works { "TripList":{ "noNamespaceSchemaLocation":"http://demo.hafas.de/bin/pub/eurail/rest.exe/v0.8/xsd?name=hafasRestTrip.xsd", "Trip":[{ "LegList":{ "Leg":[{ "idx":"0", "name":"THA 9322", "type":"THA", "reservation":"required", "Origin":{ "name":"AMSTERDAM CENTRAAL (Netherlands)", "type":"ST", "id":"8400058", "lon":"4.900829", "lat":"52.378606",

I would have expected an array even though there is one leg in the response. According to supplier this is a correct response?

Will try to create a temporary java action to fix the response before passing the response in deserializeJsonToObject java action.

mweststrate commented 9 years ago

This seems to be just unregular and weird json responses. If the provider cannot provide a stable form, we cannot reliably map it. It seems to be a bug on the provider side that the legs are nested instead of siblings. Won't fix. Thanks for reporting anyway.