189
Views
4
Comments
JSON Serialize Dynamic Key for POST API
Question

Hi,

i want to make a POST Request to an API. The API wants some static key values and some dynamic ones in the "extra_vars" as a JSON List. So there has to be the name of a variable i got from another API call before as a KEY and the Value has to be the UserInput.

The JSON POST Request should look like this in the end:

{

    "inventory": 752,

    "limit": "",

    "extra_vars": {

        "KeyDynamicA": 75,

        "KeyDynamicB": 90,

        "KeyDynamicC": ["/J2EE", "/home"]

    }

}


"inventory", "limit" and "extra_vars" are values that are fix so I can use the Structure and the ServerAction with the inputParameters made by Outsystems. But the problem for me is: The Value of "extra_vars" are dynamic pairs of keys and their values.

So in one call that could be "KeyA", "KeyB" and "KeyC" and in another call "KeyB" and "OtherKeyD"  as a JSON List in the "extra_vars". There are some calls too where extra_vars could be empty.

The Variable Type is another call I make before. So I have all values but dont know how to convert them into the right JSON List.

I tried it with a Structure with Name and Value, but the result is now: "Name": "KeyA", "Value": 75" but I need it like above. 



How can I make a dynamic key without storing all the possible keystrings that are stored in the API.



Thanks for your help!

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

Hi Markus,


Create a structure called for instance Extra_Vars with two attributes "key" and "value". Don't forget to set the names in JSON:


On your input structure you will an attribute with a list of this structure you just created with the pairs key - value.

Then on your web service, you add a OnBeforeRequest action:




On the OnBeforeRequest, you use the JSON_Objectify from ardoJson and use the path correspondent to your structure. In your case, it would be extra_vars (it can be for example, data.extra_vars if a structure is nested).


This will allow you to work with a strongly typed structure in OutSystems and to do the swap to this dynamic structure just before the call to the webservice.


Kind Regards,
João

UserImage.jpg
Markus Kucharzik

Hello João,

Thanks for your fast reply.

I tried to add the ardoJSON Dependency but if I want to compile it this error appears in the log:

Internal Error

Compilation Error.

bin\OutSystems.NssardoJSON.dll: error CS1705: Assembly 'OutSystems.NssardoJSON, Version=11.13.0.31107, Culture=neutral, PublicKeyToken=null' uses 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' which has a higher version than referenced assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

bin\Newtonsoft.Json.dll: (Location of symbol related to previous error)

State: Build References Proxy


Looks like ardoJSON is not up to date. Are there any other methods for this problem? :( 

Kind Regards,
Markus 

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

Hi Markus,


I haven't had this problem with ardoJSON, nor I have seen it in the multiple factories I have played a role.

This is however a well-known problem, particularly with this DLL. You might want to take a look at posts like this one.


Kind Regards,
João

UserImage.jpg
Markus Kucharzik

Hi João,

thanks again for your fast reply. I saw this article but I am working with Outsystems just for one month.

I dont get the problem or how to fix this :/

Why is there just a simple JSON Serialize function in Outsystems? :(


Kind Regards,

Markus

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