178
Views
15
Comments
Solved
Failed to parse response of the method

Failed to parse response of the method 

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ssTestingTool2_IS.CcParserName+RESTAlternative_country2' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.



Hi, Good day!  I am encountering this error on my API, 

Attached is my sample response.

How should I fix this?

edit: 
Is it possible that the cause of this is because the alternative_country structure has sometimes 
"alternative_countries": { US,GB,JP} and on the other data it is like this "alternative_countries":[] ?

sampleresponse.txt
2023-02-09 12-36-42
Damian Fonville
Solution

Hi,


The issue is with the alternative_countries object, Because the keys are dynamic you can't parse this to a structure or you have to create every possible country in the structure enable to parse it.

If you don;t need this field you could remove it from the sample response to generate the api, this way OutSystems skipped this field in serilization 

2023-02-09 12-36-42
Damian Fonville

In addition to my response,

The JSON is 100% valid, only native OutSystems can't handle this response, because of the dynamic json keys in alternative_countries, 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I'm pretty sure that

{ US, GB, JP }

Isn't valid JSON? For one, there's missing quotes.

2023-02-09 12-36-42
Damian Fonville


{ US, GB, JP } is indeed not valid, But I can't find this in the provided JSON file, So my guess was this was type to explain the difference between a object and an array.

But the provided JSON files is 100% correct

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
UserImage.jpg
Dianara Bathan

Hi @Damian Fonville ,


Tried to remove the field and it worked! Thanks


2020-11-25 10-45-32
Mostafa Othman
Champion

Hello Dianara,

It seems response of API is not matching your output structure of this API.

Try to test API first on service studio and copy output to output body. Also you can check service center => monitoring => integration and find error of that API.

You may also need to increase logging level of that API to get request and response then compare it to your output structure by following steps into URL: https://success.outsystems.com/documentation/11/extensibility_and_integration/set_the_logging_level_of_rest_and_soap_integrations/

UserImage.jpg
Dianara Bathan

I have done this Test the API first then copy the output to the response body, but still, I am getting the error

2020-11-25 10-45-32
Mostafa Othman
Champion

I would recommend to set logging level of that API to full as mentioned into URL I share with you then call your API one more time then go to service center => monitoring => integration and find the error of API open details and download response Json and compare it to Json returned when you test API using service studio, you may find any difference that causing issue.

2025-02-10 17-24-13
Arun Rajput

Hi @Dianara Bathan ,

Try to debug OnAfterEvent of API and check response there.

Best,

Arun 

UserImage.jpg
Poonam Verma

Hi Dianara Bathan ,


The JSON string which you are passing  to deserializing is of list type but the structure which you have assigned to deserialize action is not list type.  Kindly make it of list type and then try. 

Hope this helps you. 

2023-02-09 12-36-42
Damian Fonville
Solution

Hi,


The issue is with the alternative_countries object, Because the keys are dynamic you can't parse this to a structure or you have to create every possible country in the structure enable to parse it.

If you don;t need this field you could remove it from the sample response to generate the api, this way OutSystems skipped this field in serilization 

2023-02-09 12-36-42
Damian Fonville

In addition to my response,

The JSON is 100% valid, only native OutSystems can't handle this response, because of the dynamic json keys in alternative_countries, 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I'm pretty sure that

{ US, GB, JP }

Isn't valid JSON? For one, there's missing quotes.

2023-02-09 12-36-42
Damian Fonville


{ US, GB, JP } is indeed not valid, But I can't find this in the provided JSON file, So my guess was this was type to explain the difference between a object and an array.

But the provided JSON files is 100% correct

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
UserImage.jpg
Dianara Bathan

Hi @Damian Fonville ,


Tried to remove the field and it worked! Thanks


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Dianara,

"Is it possible that the cause of this is because the alternative_country structure has sometimes 
"alternative_countries": { US,GB,JP} and on the other data it is like this "alternative_countries":[] ?"

"{US,GB,JP}" isn't valid JSON, so if that's what is returned, it rightly fails. For one, you cannot have a list of things in an object, and secondly, if it were an array, then you'd need quotes around the values, like this:

[ "US", "GB", "JP" ]

So perhaps you can check what's the actual JSON in both cases, so we can advise you what to do.

Note that the error you get does, in general, mean that an attribute is sent polymorphic, for example, it is normally sent as above (an array), but if there's no data it's sent like {}, for example.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Ok, I missed the attachment. There's two problems here. For one, these kinds of dynamic keys can't be handled by OutSystems:

          "alternative_countries": {
            "US": 61,
            "GB": 13,
            "CA": 8
          }

OutSystems wants attributes to be fixed.

Secondly, there's a problem with "alternative_countries" being polymorphic: sometimes it's an object as above, sometimes an array:

        "alternative_countries": []

There's a number of possibilities to fix this. You can either use the On After Response and fix the JSON into something that OutSystems understands, or you just get the JSON in its entirety and use ArdoJSON to parse it (especially JSON_Objectify for the dynamic attributes). 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Since this comes up a lot on the forum, I created an Idea for it. Vote for it here.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

As a reply to my Idea, Leonardo Fernandes replied with an example how to handle your case with his AdvancedREST Forge asset.


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.