190
Views
13
Comments
Solved
How to create a JSON file
Question
Application Type
Reactive

I have related entities that I want to create a json file from. This json file contains nested data and I am not sure how to create the final json for the different scenarios of data. I would also want to download the json file when a button is clicked.
My entities hold data for OrderInfo, MedItem and MedItemDose 
How would I create the following json:

{

    "OrderInfo": {

        "OrderNum": "1677746289",        

        "Note": "",

        "DataClsf": "N",

        "MedItem": [

            {

                "MedCd": "319283006",

                "MedNm": "Amlodipine 5mg tabs",

                "UseAtcYn": "Y",

                "MedSpec": "--",

                "MedNote": "Do not take with any Grapefruit Products at the same time as this medicine.",

                "GroupNum": "1",

                "MedItemDose": [

                    {

                        "TakeDt": "20230418",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:1.00;EVENING:0;Night:0",

                        "DoseNote": "1-277-10446-1-967E;1-277-10449-1-954E;1-277-10452-1-AB3E",

                        "DrtsCd": ""

                    },

                    {

                        "TakeDt": "20230419",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:1.00;EVENING:0;Night:0",

                        "DoseNote": "1-277-10455-1-6A8F;1-277-10458-1-A91E;1-277-10461-1-EFCE",

                        "DrtsCd": ""

                    }

                ]

            },

            {

                "MedCd": "320031002",

                "MedNm": "Atorvastatin 40mg tabs",

                "UseAtcYn": "Y",

                "MedSpec": "--",

                "MedNote": "Do not take with any Grapefruit Products at the same time as this medicine.",

                "GroupNum": "1",

                "MedItemDose": [

                    {

                        "TakeDt": "20230418",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:0;EVENING:0;Night:1.00",

                        "DoseNote": "1-277-10446-1-967E;1-277-10449-1-954E;1-277-10452-1-AB3E",

                        "DrtsCd": ""

                    },                    

                    {

                        "TakeDt": "20230515",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:0;EVENING:0;Night:1.00",

                        "DoseNote": "1-277-10689-1-534;1-277-10692-1-3B44;1-277-10695-1-FAF5",

                        "DrtsCd": ""

                    }

                ]

            },        

            {

                "MedCd": "424513004",

                "MedNm": "Sitagliptin 50mg tablets",

                "UseAtcYn": "Y",

                "MedSpec": "--",

                "MedNote": "",

                "GroupNum": "1",

                "MedItemDose": [

                    {

                        "TakeDt": "20230418",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:1.00;EVENING:1.00;Night:0",

                        "DoseNote": "1-277-10446-1-967E;1-277-10449-1-954E;1-277-10452-1-AB3E",

                        "DrtsCd": ""

                    },                    

                    {

                        "TakeDt": "20230515",

                        "UserExpDate": "20230902",

                        "DoseList": "Breakfast:1.00;EVENING:1.00;Night:0",

                        "DoseNote": "1-277-10689-1-534;1-277-10692-1-3B44;1-277-10695-1-FAF5",

                        "DrtsCd": ""

                    }

                ]

            }

        ]

    }

}

2025-12-15 09-29-24
Thibaut G
Solution

Thank you for sharing.

You will have to change your approach to handle the nested list structures.
I have made a simple example, wich showcases the concept that you need to implement in your code.

In my example i'm using 3 different aggregates to collect the data just to keep it as simple as posssible. You could also use 1 DB call and use the listfilter widget.

I hope this clarifies ! 

Kind regards

Thibaut 




2024-03-22 09-17-23
Chandra Vikas Sharma


Hi Mganda,

write an action that creates a list of records  or simply fetch data from adv, sql or aggregate and then use the JSONSerialize action. 

Thanks

CV

2025-12-15 09-29-24
Thibaut G

1. Define the Structure: First, define the structure of your JSON object. (See example)

2. Populate the Object: Populate your 'OrderInfo' object with the necessary data.

3. Serialize the Object: Use the 'SerializeJson' widget to convert your 'OrderInfo' object into a JSON string.


Kind Regards

Thibaut 

2020-08-02 02-17-48
Mganda

This is the general direction i am taking. I will need more guidance to be alittle bit more specific.

  • I have created a structure from the above json named EEVEE_OUTPUT.
  • I have an aggregate that holds the data.

I then created a server action

Then created a client action on my screen that is triggered by clicking a link:

Unfortunately, the file that is downloaded on button click has nothing in it.
Where am I going wrong?


2020-08-02 02-17-48
Mganda

I added a breakpoint on my client action where my server action is on debug.
The debug path goes from the switch statement to the end instead of going through the other route(if statement, assign, json serializer ...)

2025-12-15 09-29-24
Thibaut G
2020-08-02 02-17-48
Mganda

Now my biggest problem is that am not getting all the data that I need in my Json download.
I am getting data from OrderInfo section of the Json but now the MedItem or MedItemDose which are nested arrays. The following Json is what am currently getting:

{"OrderInfo":{"OrderNum":"34","OrderDt":"03-04-2024","OrderDtm":"03-04-2024","MdctNum":"","MdctDt":"","PtntNum":"13","PtntNm":"Dominic Calvert-Lewin","Sex":"Male","Birthday":"01-01-1999","PtntAddr":"Address 1 Address 2","InOutClsf":"I","HsptCd":"","HsptNm":"","DptmtCd":"0","DoctorNm":"Dych","WardCd":"","WardNm":"","RoomNum":"","EmgClsf":"0","DschClsf":"0","OrderClsf":"0","Note":"","DataClsf":"N","DspsOpt":"5|R^Y^Y|Test Bundesliga Layout|Y","MedItem":[]}}

When I debug the application, I could see all data including those for MedItem and MedItemDose as shown in the image below:

My OML is bigger that 4MB, so can't be attached.

2020-08-02 02-17-48
Mganda

Since my OML's size is bigger that 4MB, I am sharing a link to a google drive

2025-12-15 09-29-24
Thibaut G
Solution

Thank you for sharing.

You will have to change your approach to handle the nested list structures.
I have made a simple example, wich showcases the concept that you need to implement in your code.

In my example i'm using 3 different aggregates to collect the data just to keep it as simple as posssible. You could also use 1 DB call and use the listfilter widget.

I hope this clarifies ! 

Kind regards

Thibaut 




2020-08-02 02-17-48
Mganda

I believe yours is the right process but am still experiencing similar issues where am able to get OrderInfo related info but not MedItem or MedItemDose data in my json download.

When debugging, I can see the expected MedItem and MedItemDose related data but an error pops up when stepping out

2025-12-15 09-29-24
Thibaut G
2019-09-17 11-09-41
Miguel Guerreiro

Hi Mganda.

Probably the query JSONData returns no record and therefore the execution goes straight forward to the end. Is it the case?

I cannot understand what is assigned in the "Assigns" of the TestJsonDownloadServerAction.

2020-08-02 02-17-48
Mganda

There a local variable in the server action(EEVEE_OUTPUT) whose data type is the structure. The assign is used to populate this local variable with values from the database.
Then, that local variable is used as the input to the json serializer. 

Right now, my serialized json gives me data up to DspsOpt part leaving MedItem empty and MedItemDose nonexistent.
{"OrderInfo":{"OrderNum":"35","OrderDt":"03-04-2024","OrderDtm":"03-04-2024","MdctNum":"","MdctDt":"","PtntNum":"17","PtntNm":"Bukayo Saka","Sex":"Male","Birthday":"05-05-2000","PtntAddr":"Address 1 Address 2","InOutClsf":"I","HsptCd":"","HsptNm":"","DptmtCd":"0","DoctorNm":"Arteta","WardCd":"","WardNm":"","RoomNum":"","EmgClsf":"0","DschClsf":"0","OrderClsf":"0","Note":"","DataClsf":"N","DspsOpt":"5|R^Y^Y|Test EPL Layout|Y","MedItem":[]}}

2019-09-17 11-09-41
Miguel Guerreiro

Hi Mganda.

I found some issues:

  1. Each action/function should do only one "task". JSONDataServerAction is not only returning the JSON, it's calling the action Deactivate_Order. The call of the action Deactivate_Order should be done in the MainFlow\ProductionList\DeactivateOrderAndDownloadJson .
  2. The query JSONData is only returning 50 records, probably you want to return more data, right?
  3. In the image bellow:The IF that I identify as 1, is not doing nothing. If the list is empty, it will step out the for each. The flow ID that I identify as 2, is duplicated, the only difference is "EEVEE_OUTPUT.OrderInfo.MedItem.Current.GroupNum" is "1" when JSONData.List.Current.GLOBAL_VMP.Id <> NullIdentifier(). You can have only a flow and put an inline IF as If(JSONData.List.Current.GLOBAL_VMP.Id <> NullIdentifier(), "1", "") in the "EEVEE_OUTPUT.OrderInfo.MedItem.Current.GroupNum" value.
  4. Regarding the mapping, I agree with @Thibaut G, you should revisit your approach to handle the nested list structures.
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.