256
Views
4
Comments
Solved
Deserialize JSON with Dynamic Key from REST API
Application Type
Reactive
Platform Version
11.20.0 (Build 38549)

Hi, I am consuming a REST API which returns JSON data that represents entity tables. The problem is it's returning each row as a dynamic key, with the row id as the key. Example of the response:

{

  "entity1": {

    "0": {

      "attribute1": "value",

      "attribute2": "value",

      "attribute3": "value"

    },

    "1": {

      "attribute1": "value",

      "attribute2": "value",

      "attribute3": "value"

    }

  },

  "entity2": {

    "0": {

      "attribute4": "value",

      "attribute5": "value",

      "attribute6": "value"

    },

    "1": {

      "attribute4": "value",

      "attribute5": "value",

      "attribute6": "value"

    }

  }

}

The "0", "1", key represents the row of the entity, but since it wasn't put in list despite having same attributes, OutSystems default JSON deserialization takes each row as an object and produces structure for each fo it, 

My goal is to display each entity as a table, so the desired structure is just an Entity structure with its attributes, instead of having a structure for each entity record row. How should I achieve this? Do I take the response in text first, and then format the response to deserialize it manually into my desired structure? The structure of the JSON response could not be modified. Sorry for the long question. Please shine a light on me, thank you.

 


2018-10-29 08-31-03
João Marques
 
MVP
Solution

Hi Max Chia,


OutSystems is a strongly-typed language meaning you have to define the structure before hand. However, you can transform that dynamic list of attributes in a list of key-value pairs using JSON_Listify action from ardoJSON.

Take a look at the Integrating dynamic structures with OutSystems article which explains step by step how you can apply it, with a practical example.


Kind Regards,
João Marques

2021-09-06 15-09-53
Dorine Boudry
 
MVP
Solution

Hi @Max Chia ,

i think yes, if you can't make the producer of the api change the format, you'll have to do some custom work yourself on your end.

Either as you say, define it as a text on your end, and then manually convert and deserialize it.

Another, similar, option is to add a OnAfterRespons to your consumed API definition, and do some transformations there to make it digestable by Outsytems.  Then you can take it into your flow as a structure and you don't have to deserialize by hand.

But a related question that comes to mind, how do you think of dealing with all different entity definitions ? are you happy with having to define them explicitly on your end as different structures ?  If that's ok (i.e. the entities and attributes are well defined and stable), both options work, if not, you'll have to take it in as text and parse entirely with custom logic, I think.

Dorine

2018-10-29 08-31-03
João Marques
 
MVP
Solution

Hi Max Chia,


OutSystems is a strongly-typed language meaning you have to define the structure before hand. However, you can transform that dynamic list of attributes in a list of key-value pairs using JSON_Listify action from ardoJSON.

Take a look at the Integrating dynamic structures with OutSystems article which explains step by step how you can apply it, with a practical example.


Kind Regards,
João Marques

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

Hi,

in addition to Dorines and Joaos answer you may find this component https://www.outsystems.com/forge/component-overview/12401/jsonata-transformation helpful (iam the creator). 

Stefan

2023-03-16 04-58-56
Ashish Lonkar

Hi Max,

you can use ardoJSON for this. The key is to take the dynamic attribute in key-values list form and then maniplulate them through OnAfterResponse.

Refer this article from Joao I found it very useful, 

https://medium.com/@jsmarques13/integrating-dynamic-structures-with-outsystems-6c45e36a4d47

Also you can check out these similar dicussions,

https://www.outsystems.com/forums/discussion/62730/consume-rest-api-with-a-dynamic-list-inside-one-object/

https://www.outsystems.com/forums/discussion/54898/manipulate-dynamic-json/


Thanks AWL!

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