How is your experience with the OutSystems Community?
43
Views
10
Comments
Construct complex nested JSON file
Question

Hello all, 

I am needing to generate a complex JSON structure that follows this format: 


Here is the output I am getting with my current action flow in OutSystems: 


The issue is that the "phone_numbers" attribute is not populating correctly, or at all. Here is my action flow for serializing the JSON file: 

The 'PhNum' variable is of type PhoneNumbers List (see structure below).

OutSystems Structure Layout: 


If i map the PhNum like so (see below) the JSON output file fills the "phone_numbers" attributes with ALL of the phone numbers as opposed to the unique phone_number entry per record in the entity. : 


All in all, I am hoping someone can assist me in sorting out the method of getting the phone number value for each user in the main entity and assigning it to the phone_numbers attribute in the JSON structure, per user. 


Let me know if more info is needed, I am new to OutSystems, and am not entirely sure what all information is needed in order to provide assistance. 


Thanks.

UserImage.jpg
vikas sharma
Champion

Hi

At high level structure seems fine. Basically instead of taking array of phone numbers could you try with array of text. Phone_numbers array change datatype to array of text and store phone number in text. 

Regards 

2023-09-06 07-26-35
Sudip Pal

Hey Carson,

If possible can you change the JSON structure like

"phone_numbers":[

        {"PhoneNumbers":"1234567890"}

]

Hope this will help you !

Thanks ,

Sudip Pal

2022-11-18 15-24-49
Carson Braun

Hey Sudip, 

Thanks for the reply.

Theoretically, I could change the structure to match what you have mentioned. However, the target WebServier is very particular about the format of data to be received. If the JSON structure is not formatted the proper way, it will not take the data. 


Thank you, though! 

Carson

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Carson,

Can you share a module? I think the problem has little to do with JSON, and everything with the way you assign things. But from your images it's difficult to check what's going on.

2022-11-18 15-24-49
Carson Braun

Thanks for the quick response. I am sure that you are correct in the sense of it is down to how I am assigning things in the module. Here is the Module I am working with. The action flow I had in the image above is located in the "Load_JSON_TEST" screen action in the MainFlow/AccountList page. 


JSONStruct_Help.oml
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Carson,

Ask yourself, in this assignment, what are you assigning?



"GrowerEntity.List" is the output of the "GrowerEntity" Screen Aggregate, and you are mapping it 1:1 to the Users attribute. What effectively happens, is that for each element (record) in the GrowerEntity.List, a new record is created in the Users list, with as attributes the attributes of GrowerEntity.List.Current. However, for Phone_numbers, you are not assigning something from the Current of GrowerEntity.List, but instead a global (to the assignment) variable PhNum. Which means, that for each Users record, you'll be adding the same list of phone numbers, as PhNum isn't iterated over. PhNum contains, because of the assignment, the phone numbers of all records in GrowerEntity, so each Users record gets all phone numbers.

Now, to solve, this, ideally you could do something like this:

since there's only one phone number in GrowerEntity. However, it's a well-known bug in Outsystems that this doesn't compile (and also there's a presentation error, as the [0] should be indented at Phone_numbers level).

So the solution is to not use mapping, but use a For Each loop over GrowerEntity.List, and add Users records "manually" by assigning the attributes of a Structure variable, then using ListAppend to append it to Users.

That all said, why are you serializing the structure yourself, instead of letting OutSystems handle it? Is it because the Bushel API needs binary data instead of JSON as input?


2022-11-18 15-24-49
Carson Braun

Thanks again for the quick reply! 

This is very helpful, and I will try the ForEach/ListAppend method. 


To answer the question as to why I am serializing the structure instead of letting OutSystem handle it; I am not too familiar with how OutSystems handles serializing structures, and doing it myself seemed easier haha. 

The Bushel API input only needs a JSON file in the specific format. No requirement for it to be binary data.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

In that case, you should really not serialize the JSON yourself, but instead follow the best practices, as described here. I strongly advise you to convert your code to use that. (Note that this doesn't matter with regards to the mapping/ListAppend problem discussed above, so you can try to solve that first.)

2022-11-18 15-24-49
Carson Braun

Hello Kilian, 

Sorry for the late response. 

I have gone ahead and refactored my application to use the OutSystems JSON serialization. That is looking good. however, my issue now is assigning the entity data to the new JSON structure in order to post it to the consumed API. 

Would I use a ForEach loop to iterate over the entity, and assign each record to the structure? 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Carson,

Yes, a For Each and a ListAppend to add the record. Note that since you'll also need a ListAppend to add the single phone number from GrowerEntity.List.Current.Grower, before you ListAppend the structure to the list, you also need to ListClear PhoneNumbers, as otherwise you'll keep adding to an ever longer list.

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