203
Views
8
Comments
How to map REST API that returns dictionary of key value pair
Question

I want to consume an external REST API and the response contains a dynamic dictionary:

[

  {

    "id": "123",

    "properties": {

      "TestProperty1": "TestValue1"

    }

  },

  {

    "id": "345",

    "properties": {

      "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.

2021-10-09 07-57-44
Stefan Weber
 
MVP

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

UserImage.jpg
Adelia Benalius

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!

2021-10-09 07-57-44
Stefan Weber
 
MVP

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

2022-08-03 04-32-50
Ravi Punjwani

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.

UserImage.jpg
Adelia Benalius

@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?

2022-08-03 04-32-50
Ravi Punjwani

Hi @Adelia Benalius 

Were you able to resolve you query with the component suggested above that had the documentation and working sample? 

UserImage.jpg
Adelia Benalius

hi @Ravi Punjwani sorry not yet, i'm looking into something else and don't have time yet to follow up in this

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