I want to consume an external REST API and the response contains a dynamic dictionary:
[
{
"id": "123",
"properties": {
"TestProperty1": "TestValue1"
}
},
"id": "345",
"TestProperty2": "TestValue2"
]
How can I map the properties field as a list of keyvalue pair? Outsystems automatically convert it into a structure with the fixed property names.
I have created a forge component for complex transformations of JSON. See here https://www.outsystems.com/forge/component-overview/12401/jsonata-transformation
An example on how to transform a dictionary to key values is in the documentation section.
STefan
Hi @Stefan Weber thanks for the pointer! I'm very new to Outsystems, so i need some more help with this :)
Where do I use the server action? I thought I should use it in the On After Response of the API call, but that event is only available on the main API level and not on a specific API call.
Should I create the structure for the API response in the after-transform format?
Thanks!
Hi @Adelia Benalius.
At your REST consume action you add an output property set to text data type. This gives you the result JSON of the API in plain text form.
In your server action you use the transform of the JSONata component to transform it, giving you another plain text JSON string. You can then deserialize that string to the real structure you need. To see how this is applied, you can download one of my components like this one https://www.outsystems.com/forge/component-overview/12610/hashicorp-vault-key-value-engine
Stefan
Hi @Adelia Benalius ,
Although you can try out the solution Stefan suggested, I would also recommend to try out this component.
https://www.outsystems.com/forge/component-overview/9567/advanced-rest
Install it and also install its demo app from your service studio.
When you install the demo app, open it on the browser and scroll down to the bottom of Home page.
You will find exact example that you want to fix.
Click on Transform JSON and see how that conversion happens. Then open the code, follow the same steps in your application, as implemented in the demo app's code. You can use it inside OnAfterResponse too.
@Ravi Punjwani where can I find the demo application? I searched in Forge and the author's Forge component list but can't find it. could you send me the link?
Hi @Adelia Benalius
Were you able to resolve you query with the component suggested above that had the documentation and working sample?
hi @Ravi Punjwani sorry not yet, i'm looking into something else and don't have time yet to follow up in this