Search code examples
jsonreportsubreporttelerik-reporting

In Telerik Reporting (Desktop), how to inject a json sub-node into sub-report string parameter


Introduction

Based on the below json sample, I am trying to define a Telerik report using both a main report and a sub-report, where se second is fed with a subset of the main report json data.

Basically, data flow mimics something like:

  1. Application injects base json data into main report's jsonData(string) parameter;
  2. jsonData is binded as report datasource;
  3. Json data selector is applied;
  4. Json selected data Employes node is injected into a sub-report, which will also use it as a json data source.

Troubles arose on step 4, where I am getting an [Invalid value of report parameter 'jsonData'] error when main report is rendered.

It seems Telerik Report is unable to convert a selected json node field data into a proper string.

Full context

For full context:

  • On both reports:

    • There is a jsonData report parameter of string datatype;

    • There is a JsonDataSource defined;

    • Binding between the string jsonData parameter and main JsonDataSource is based upon:

      • Property path: DataSource.Source
      • Expression: = Parameters.jsonData.Value
  • On main report:

    • JsonDatasource $.Companies data selector is being used, resulting in a list representing containing de the companies list, each instance having:
  • Name, a string representing the company name;

  • Employes, an object containing the company employes list.

    • Full sample json data is injected thru jsonData parameter as a string;
  • On sub-report:

    • There is also a jsonData report parameter of string datatype;
    • JsonDataSource $ data selector is being used, which should result in:
      • A list of the following fields:
        • Name, a string representing the name of the employe;
        • Wage, a numeric value representing employe's wage.
  • Again on main-report, I am using the following sub-report parameters mapping:

    • Parameter Name: jsonData
    • Parameter Value: Fields.Employes
  • I am using Telerik Report Designer v15.1.21.716 (Desktop)

It seems that data selector converts json Employes node date into a System.Object[], which is giving me a nice hard time figuring out how to convert it back to a json string.

I have alread extensively searched on documentation, web, ChatGPT and alikes for a valid solution. So far, no luck.

Before you help me

Although I have the most appreciation for anyones effort trying to helping me:

  • I am pursuing a json only data source solution. I mean, I am not interested on any other alternative suported Telerik Reporting datasources;
  • Solution must work in both design-time and runtime.
{
    "Companies": [
        {
            "Name": "Company1",
            "Employes": [
                {
                    "Name": "Joe",
                    "Wage": 1000
                },
                {
                    "Name": "Jack",
                    "Wage": 2000
                }
            ]
        },
        {
            "Name": "Company2",
            "Employes": [
                {
                    "Name": "Mary",
                    "Wage": 3000
                },
                {
                    "Name": "Mike",
                    "Wage": 4000
                }
            ]
        }
    ]
}

Solution

  • Indeed, passing the "Employes" object to the "jsonData" parameter of the subreport should not work because the object cannot be converted to a JSON string directly. You have to convert it thru CStr() function.

    However, instead of using a parameter to set the data source of the subreport, you can use the binding described in the How to Represent Hierarchical Nested Data KB article.

    I also want to mention that in the R3 2022 SP1 release, the DataSource property was added to the SubReport item as well. This makes the approach I suggested above obsolete because it allows you to bind the data source of the subreport directly in the main report. For example:

    Solution 1

    SubReport item binding based on DataSource property:

    Property path: DataSource
    
    Expression: = Fields.Employes
    

    Solution 2

    In @Julio's provided sample, which allows to inject json node as a sub-report parameter, instead of a direct datasource, you should use following approach.

    SubReport item binding thru sub-report parameter:

    Property path: jsonData
    
    Expression: = CStr(Fields.Employes)
    

    Bear in mind that...

    • Solution 1, although does not require an explicit report to sub-report binding, it still needs the following inner binding on the sub-report:

      Property path: DataSource

      Expression: = ReportItem.Parent is Null ? ReportItem.DataSource : ReportItem.Parent.DataObject.Employes

    • Solution 2, requires additional binding between the sub-report parameter and its datasource, as stated on the original post.

    For further details on this subject, check original Telerik's original post