Hi Team,I am consuming a REST API in OutSystems 11 and have noticed that the response JSON structure is not always consistent.For example, sometimes the API returns 10 attributes:{ "id": 1, "name": "John", "email": "john@test.com", "phone": "1234567890", "department": "IT"}
And sometimes it returns only a subset of those attributes:{ "id": 1, "name": "John"} What is the recommended approach in OutSystems 11 to consume such dynamic JSON responses without causing deserialization errors? Is it safe to generate the response structure with all possible attributes? Should the response be consumed as Text and parsed manually? Are there any best practices or Forge components that can help handle this scenario efficiently? I would appreciate any guidance or examples from similar implementations. - Thanks in advance!
Hi @Pradip Chavhan,
I don't think you can dynamically assign the response structure. When the API response is returned, OutSystems maps it directly to the structure defined for the REST API response.
If you are consuming the API directly, you could instead serialize the response first and then deserialize it into the required structure, which would give you more control over the attributes being returned and how they are handled.
Alternatively, you can create a structure containing all the expected attributes and map the API response directly to that structure. This is safe, even if some attributes are not returned by the API, but it can require additional effort to maintain if the API structure changes frequently.
Another option, if you only need a subset of the API attributes in your system, is to create a local Entity containing only the attributes that are relevant to your application. Once the API returns the response, you can map the required values to the Entity and store them there. This can be a cleaner approach when you only need specific information from a larger or changing API response.
Hi @Pradip Chavhan ,See this Post -https://www.outsystems.com/forums/discussion/86704/deserialize-json-with-dynamic-key-from-rest-api/https://www.outsystems.com/ideas/13507/rest-better-handling-of-dynamic-and-polymorphic-attributes/
Thank you all for your valuable suggestions and insights.I appreciate the time you've taken to review my question and provide guidance. I will go through the recommendations , evaluate the suggested approaches, and implement the most suitable solution for my use case.
Thanks again for your support and assistance.
Hi Pradip,
You need to distinguish between attributes that are in the response Structure in OS and/or attributes that are sent but are not in the response Structure in OS, and truely dynamic attributes and/or polymorphic attributes.
For the first situation it's important to know that OutSystems will try to match the Structure attributes with the key values in the JSON. If it doesn't find a key in the Structure, it just skips that key. If there's an attribute not in the JSON, it simply will ignore that attribute. So extra keys or a lack of keys will not cause deserialization errors.
The second situation is more difficult to handle. The links above posted by Rahul give some options.
You can create a large structure that the OS can map itself. if it can't found the key, it will skip mapping for this key-value.
Yes, that is what I wrote yesterday ;).