172
Views
7
Comments
Solved
Extract only relevant information from dynamic JSON
Question

Hello,

I have the following JSON structure returned by a REST API. The issue I'm facing is that under ApprovalSettings attribute sometimes I receive a simple object, sometimes an array which cannot be correctly mapped to an OutSystems structure (the JSON is dynamic).

I am wondering what is the easiest way to extract only the relevant information? I changed the response to a Plain Text and tried using ardoJSON's JSON_Listify() action, but I cannot seem to get the result I want. Maybe there are some other ways of achieving this.

The only information relevant for me is the UserID attribute.

Regards,

Bogdan

{

    "Users": [

        {

            "ExternalID": "string",

            "ClientID": 0,

            "ClientName": "string",

            "GroupID": 0,

            "PartnerID": 0,

            "Username": "string",

            "Status": "string",

            "StatusReason": "string",

            "Expire": "2023-03-27T16:08:31.656Z",

            "Created": "2023-03-27T16:08:31.656Z",

            "GroupName": "string",

            "PartnerName": "string",

            "LifetimeCampaigns": 0,

            "LifetimeEmails": 0,

            "UserID": 0,

            "Role": "string",

            "RoleID": 0,

            "FirstName": "string",

            "LastName": "string",

            "Email": "string",

            "PasswordChange": "2023-03-27T16:08:31.656Z",

            "LastLogin": "2023-03-27T16:08:31.656Z",

            "Last12MonthsCampaigns": 0,

            "Last12MonthsEmails": 0,

            "Last30DaysCampaigns": 0,

            "Last30DaysEmails": 0,

            "Last30DaysEmailAverage": 0,

            "Last30DaysEmailChange": 0,

            "Last30DaysCampaignsChange": 0,

            "Last30DaysCampaignsAverage": 0,

            "SubscriptionEstimatedUsage": 0,

            "SubscriptionEstimatedDifference": 0,

            "SubscriptionSettings": [

                {}

            ],

            "ApprovalSettings": [

                {}

            ]

        }

    ],

    "ResultCode": 0,

    "ResultDescription": "string",

    "TotalResults": 0

}

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

Hi @Bogdan Boglea ,

just checking if I get this right : OS has problems because ApprovalSettings has a different structure on different calls.  And as a consequence, you sometimes run into parse errors.

But you are not interested in anything that can be found inside ApprovalSettings ?

In that case, just avoid it getting parsed, as any information in the JSON OS is not looking for, is ignored and structure changes are harmless.

You can do one of these 2 things, after first adding the consumed REST api with full structure (so no need to make response a plain text) :

  • remove the ApprovalSettings part of the structure that OS created
  • if you think that's too drastic, leave it but change the 'name in JSON' property to something like ignore_ApprovalSettings.

Or you could avoid OS ever even making any structures for this, by editing it out of the example response before doing 'copy to response body'.

Be sure to document this well, as anybody working on that REST api in the future will want to know about this.

Dorine 

2024-01-18 12-42-28
Bogdan Boglea

Hi Dorine,

Amazing, not sure why I didn't think of this, I remember trying to remove some attributes from the response structure but encountered some issues.

Now with a clear mind I just removed the ApprovalSettings structure and it seems the API works as expected.

Thanks a lot,

Bogdan

2019-11-08 16-23-42
César Mateus Conceição

Hi

I did quick page where you can test your json.
If it's what you expected I send you this example.

My Page: https://cesar-mateus.outsystemscloud.com/WebAppsTests/TestJSON?_ts=638155395786049590


TESTED JSON

{

  "Users": [

    {

      "ExternalID": "string",

      "ClientID": 0,

      "ClientName": "string",

      "GroupID": 0,

      "PartnerID": 0,

      "Username": "string",

      "Status": "string",

      "StatusReason": "string",

      "Expire": "2023-03-27T16:08:31.656Z",

      "Created": "2023-03-27T16:08:31.656Z",

      "GroupName": "string",

      "PartnerName": "string",

      "LifetimeCampaigns": 0,

      "LifetimeEmails": 0,

      "UserID": 105,

      "Role": "string",

      "RoleID": 0,

      "FirstName": "string",

      "LastName": "string",

      "Email": "string",

      "PasswordChange": "2023-03-27T16:08:31.656Z",

      "LastLogin": "2023-03-27T16:08:31.656Z",

      "Last12MonthsCampaigns": 0,

      "Last12MonthsEmails": 0,

      "Last30DaysCampaigns": 0,

      "Last30DaysEmails": 0,

      "Last30DaysEmailAverage": 0,

      "Last30DaysEmailChange": 0,

      "Last30DaysCampaignsChange": 0,

      "Last30DaysCampaignsAverage": 0,

      "SubscriptionEstimatedUsage": 0,

      "SubscriptionEstimatedDifference": 0,

      "SubscriptionSettings": [

        {}

      ],

      "ApprovalSettings": [

        {}

      ]

    },

    {

      "ExternalID": "string",

      "ClientID": 0,

      "ClientName": "string",

      "GroupID": 0,

      "PartnerID": 0,

      "Username": "string",

      "Status": "string",

      "StatusReason": "string",

      "Expire": "2023-03-27T16:08:31.656Z",

      "Created": "2023-03-27T16:08:31.656Z",

      "GroupName": "string",

      "PartnerName": "string",

      "LifetimeCampaigns": 0,

      "LifetimeEmails": 0,

      "UserID": 101,

      "Role": "string",

      "RoleID": 0,

      "FirstName": "string",

      "LastName": "string",

      "Email": "string",

      "PasswordChange": "2023-03-27T16:08:31.656Z",

      "LastLogin": "2023-03-27T16:08:31.656Z",

      "Last12MonthsCampaigns": 0,

      "Last12MonthsEmails": 0,

      "Last30DaysCampaigns": 0,

      "Last30DaysEmails": 0,

      "Last30DaysEmailAverage": 0,

      "Last30DaysEmailChange": 0,

      "Last30DaysCampaignsChange": 0,

      "Last30DaysCampaignsAverage": 0,

      "SubscriptionEstimatedUsage": 0,

      "SubscriptionEstimatedDifference": 0,

      "SubscriptionSettings": [

        {}

      ],

      "ApprovalSettings": [

        {}

      ]

    }

  ],

  "ResultCode": 0,

  "ResultDescription": "string",

  "TotalResults": 0

}

2024-01-18 12-42-28
Bogdan Boglea

Hi Cesar,

Seems it works for your example, could you maybe provide the OML so I can also try it out with some real data and see the implementation?

Thanks a lot,

Bogdan

2019-11-08 16-23-42
César Mateus Conceição
2024-01-18 12-42-28
Bogdan Boglea

I think not needed anymore, but thanks for your efforts.

Dorine's idea did the job :D

2019-11-08 16-23-42
César Mateus Conceição

Great! Bogdan
Always can count with us 🙂


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

Hi @Bogdan Boglea ,

just checking if I get this right : OS has problems because ApprovalSettings has a different structure on different calls.  And as a consequence, you sometimes run into parse errors.

But you are not interested in anything that can be found inside ApprovalSettings ?

In that case, just avoid it getting parsed, as any information in the JSON OS is not looking for, is ignored and structure changes are harmless.

You can do one of these 2 things, after first adding the consumed REST api with full structure (so no need to make response a plain text) :

  • remove the ApprovalSettings part of the structure that OS created
  • if you think that's too drastic, leave it but change the 'name in JSON' property to something like ignore_ApprovalSettings.

Or you could avoid OS ever even making any structures for this, by editing it out of the example response before doing 'copy to response body'.

Be sure to document this well, as anybody working on that REST api in the future will want to know about this.

Dorine 

2024-01-18 12-42-28
Bogdan Boglea

Hi Dorine,

Amazing, not sure why I didn't think of this, I remember trying to remove some attributes from the response structure but encountered some issues.

Now with a clear mind I just removed the ApprovalSettings structure and it seems the API works as expected.

Thanks a lot,

Bogdan

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