Output to Local Variable from RunJavaScript

Output to Local Variable from RunJavaScript

  

Hi all,

I have a local variable (text) that I want to manipulate with the RunJavaScript server action in the back-end. The JavaScript should be actioned server-side rather than in the browser.

I have a text local variable that I want to take as input and run JavaScript to manipulate it before returning it as an output variable to use in an assign action.

As the image shows, the input (text) variable is JSONSerialize1.JSON, which needs to be manipulated in the RunJavaScript action then outputted as a variable to be used in the Assign action.

Is there an in-built action to do this as I can't find a way to do it with JavaScript? I am aware that RunJavaScript may not be the best action to use here but I can't find an appropriate one. 

Thanks very much,

Craig

Hello Craig,

JavaScript is a browser script language, not a Server side language. 

If you need to manipulate a JSon string, you can do that server side on OutSystems, as there are some basic text manipulation functions as well as a text utils eSpace with any functions and support to regex and so on.

If this isn't enough, you can create an extension and use C#(dot net) or java, depending on your stack, to do whatever you need to the string.

There is no need to use JavaScript server side.

I can't open the image now, but if you tell in which ways you need to manipulate the string, we can give you sore specific answer.

Cheers,

Eduardo Jauch

Thanks Eduardo.

It is a nested JSON:

{"key1":"1", "key2":{"id":"1", "KeyN": "2"}, "key3": "3"}

The issue is that the JSONSerializer in OS treats the nested JSON file as string rather than a JSON file. I want to delete the id field in the nested JSON file by:

delete key2.Id

And I have managed to turn it from string to JSON.

Thanks,

Craig

Hi Craig.

I think I'm not following.

From what I got from the image and from what you said:

JSONSerializer will receive as input a record or a list, and will return a JSON formatted string.
So, if the output of the Serializer is something like this: {"key1":"1", "key2":{"id":"1", "KeyN": "2"}, "key3": "3"}, you are saying that the record you are providing as input to the Serializer have a structure like this:

Record
|_ Key1 = 1
|_ Key2 (structure/record type)
       |_ id = 1
       |_ KeyN = 2
|_ Key3 = 3

If you simply "delete" Key2.Id, assuming that you are talking about the JSON generated by the Serializer, you will still have something like this: {"key1":"1", "key2":{"KeyN":"2"}, "key3": "3"}

If you are trying to do this because something you are doing with it that is not being recognize, probably it will not work...

Because the structure you are providing to the Serialize is a "complex" structure, it can't come directly from an Aggregate or an Advanced SQL. So, you need to have this structure defined somewhere in your application. If you want to change Key2 to be like Key1 and Key3, why don't you just change the structure (or create a new one and populate it) to be something like this:

Record
|_ Key1 = 1
|_ Key2 = 2
|_ Key3 = 3

And the result of the Serializer will be what you need?
What are you using that requires a JSON formatted string, but don't accept JSON structures in it?

Cheers,
Eduardo Jauch

Eduardo Jauch wrote:

Hi Craig.

I think I'm not following.

From what I got from the image and from what you said:

JSONSerializer will receive as input a record or a list, and will return a JSON formatted string.
So, if the output of the Serializer is something like this: {"key1":"1", "key2":{"id":"1", "KeyN": "2"}, "key3": "3"}, you are saying that the record you are providing as input to the Serializer have a structure like this:

Record
|_ Key1 = 1
|_ Key2 (structure/record type)
       |_ id = 1
       |_ KeyN = 2
|_ Key3 = 3

If you simply "delete" Key2.Id, assuming that you are talking about the JSON generated by the Serializer, you will still have something like this: {"key1":"1", "key2":{"KeyN":"2"}, "key3": "3"}

If you are trying to do this because something you are doing with it that is not being recognize, probably it will not work...

Because the structure you are providing to the Serialize is a "complex" structure, it can't come directly from an Aggregate or an Advanced SQL. So, you need to have this structure defined somewhere in your application. If you want to change Key2 to be like Key1 and Key3, why don't you just change the structure (or create a new one and populate it) to be something like this:

Record
|_ Key1 = 1
|_ Key2 = 2
|_ Key3 = 3

And the result of the Serializer will be what you need?
What are you using that requires a JSON formatted string, but don't accept JSON structures in it?

Cheers,
Eduardo Jauch

The image I sent is a tad misleading - my apologies. I have a table (list) that I use to create the first JSON with JSONSerializer1 in the stucture {1,2,3}. Then I have a local variable that is used to create a second JSON with JSONSerializer2 in the structure {a,{1,2,3},b} so the output from JSONSerialize1 is nested within JSONSerialize2. So this results with a structure within a structure, which I didn't think was possible?

By nesting JSONs within one another using JSONSerializer, it seem to produce a string within a JSON, so that {1,2,3} is seen as string rather than an JSON object. This makes it hard to manipulate (as in I can't call a key in that JSON because it's string). This is just being used for a simple API but I've managed to get myself in a muddle!

Thanks so much for the help.


Solution

Ok.

What I don't understood yet is if you want to have a nested JSON or if you just want to append the second list into the first.

In the first case, the better (and by far the easiest) would be to create a Structure that mimics your List record, adding the field to store the nested object (data type itself a list of another structure or structure). You create a local variable of type List Structure and fill the list with the data from your table, leaving the new field (not in the table) empty. Than you fill this field (list/structure) with data from the second list/table/record.

Than you do a single JSONSerialize and gets as result a fully working JSON formatted string with nested JSON objects.

If this is not what you want, but rather only "append" more elements, you just use the ListAppend to append them to the list before serializing. Again, you do the JSONSerialize only once.

It is easier (or at least more understandable and obvious) than manipulate strings.

Cheers,
Eduardo Jauch

Solution