REST: better handling of dynamic and polymorphic attributes
927
Views
4
Comments
New
Data & Integrations

One common scenario in various popular REST APIs is objects with dynamic attributes, and also the way empty instances of such objects are sent.

With dynamic attributes, I mean something like this:

"AvailableLocales" : {
   "nl-NL" : "Netherlands Dutch",
   "pt-PT" : "Portugal Portuguese",
   "en-UK" : "British English"
}

Currently, you need an On After Response and the ardoJSON extension to interpret this. It would be better if there would be an option to turn this into a key/value pair list directly.

Secondly, quite often, an empty list like the above is denoted with the empty array symbol:

"AvailableLocales" : []

Even if the dynamic attributes are a closed class, and you'd manage to make all into an attribute, this results in a parsing error. Instead of an error, the Platform should recognize this as an empty array or object, and not give a parse error.


You don't need to use ardoJSON. You can use the AdvancedREST extension to expand this JSON object into an array of key/value pairs.

Use JSON_ConvertObjectToKeyValuePairList inside the OnAfterResponse. It receives a JSON (which should simply be your ResponseText), and a path to the object you want to expand. It outputs a transformed JSON which you can then assign to ResponseText and have it deserialized into a structure.


For example, take the payload:

[{ "Name": "App1", "AvailableLocales": { "nl-NL": "Netherlands Dutch", "pt-PT": "Portugal Portuguese", "en-UK": "British English" } }, { "Name": "App2", "AvailableLocales": { "en-UK": "British English" } }]

Executing the JSON_ConvertObjectToKeyValuePairList with it as input and the ObjectPath "AvailableLocales" will output the following JSON:

[{ "Name": "App1", "AvailableLocales": [{ "key": "nl-NL", "value": "Netherlands Dutch" }, { "key": "pt-PT", "value": "Portugal Portuguese" }, { "key": "en-UK", "value": "British English" }] }, { "Name": "App2", "AvailableLocales": [{ "key": "en-UK", "value": "British English" }] }]

This output JSON can be deserialized into a structure without any problems.


The inverse operation, JSON_ConvertKeyValuePairListToObject, also exists, and can be helpful to invoke REST services that require a polymorphic object as input parameter.

Ah, yeah, I keep forgetting your nifty Forge asset Leonardo :). Nevertheless, it would be better if it were natively supported, as it's a very common scenario.

Using JSONata Transformation at OnAfterResponse is also a solution for this.
https://www.outsystems.com/forge/component-overview/12401/jsonata-transformation

Good idea! We've had a similar situation in which we needed to show all the values in the JSON in a pre-filled form and map them to descriptions of the fields, based on a JSONPath key. For that we've written a function to find a value based on a 'JSONpath'.