Import Json into a Nested Report (Non-Composite)

Ramón San Félix Ramón
CODE AUTHOR
Posts: 36
 1 week 1 day ago #600 by Ramón San Félix Ramón
Ramón San Félix Ramón created the code: Import Json into a Nested Report (Non-Composite)

In certain situations, it may be necessary to import data from a JSON file into a Nested Report within a DataWindow in PowerBuilder, for instance, when receiving data from an API.
This task is currently quite complex and requires applying some advanced techniques to function correctly.

In this article, I present a complete and functional solution that allows importing JSON data and displaying the nested content correctly across all pages of the DataWindow, both during development (IDE) and in the compiled executable version.

Step-by-Step Solution:

1. Create an Auxiliary DataStore
Assign the same DataObject to this DataStore as the one used by the Nested Report's DataWindow. This auxiliary DataStore will be used to temporarily import the JSON data.

2. Obtain the Original Data from the DataWindow
Save the current data of the object to restore it later:

ls_data_original = ds_aux.Describe("DataWindow.Syntax.Data")

3. Import the JSON into the Auxiliary DataStore

Use ImportJson() to load the JSON data.

4. Extract the Data Again
After importing the JSON, retrieve the DataWindow's data, which now includes the imported data:

ls_data_new = ds_aux.Describe("DataWindow.Syntax.Data")

5. Export Considering the Environment (IDE or Executable)

When working with object syntax, it's important to detect whether you're in the development environment or running a compiled application to correctly handle the library (.pbl) paths. If using the IDE, LibraryExport() will be used; in the compiled version, you'll need to create the file by composing the export header and the DataWindow syntax.

6. Combine Syntax with Data and Import the Object
Use the newly generated syntax to temporarily replace the DataWindow object in the library. This approach effectively "injects" the data directly.

7. Import the JSON into the Base DataWindow
With the Nested Report object modified, now import the data into the main DataWindow.

8. Use the Trick of Processiong=5 and the GetChild() Function
This step allows replacing the retrieve arguments used in the Nested Report with their actual values:

dw_report.Modify("DataWindow.Processing=5")
dw_report.GetChild("dw_cabecera", dwc_Nested)
wf_set_args(dwc_Nested, ...)

9. Restore the Original Object Syntax
Once the process is complete, restore the original object to leave the library unaltered.

Practical Example

In today's example, we have a DataWindow with a header containing a Nested Report with two computes and two rows. This setup allows us to verify that the data from the nested.json file has been correctly imported and that the retrieve arguments have been properly replaced with their values.

The compute with the logo uses the value from the company column, and the compute with the company name uses the company argument.

Everything appears to function correctly, both in the IDE and in the compiled version.

Note: You would also need to change the arguments to their values in the base DataWindow after the import; in my example, I created it without arguments, so no additional steps were necessary.

I leave you here a copy of the project today but I always recommend that you download it from git hub in case there is an update:

github.com/rasanfe/TestNested

To be aware of what I publish you can follow my blog in Spanish:

https://rsrsystem.blogspot.com

 

Spoiler: If you compile this example in PowerClient, it won't work out of the box. You'll need to apply a few extra tricks, which I’ll be sharing during my talk at the Appeon PowerBuilder Regional Conference Spain 2025, on April 22 in Madrid.

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.