How to bind to JSON returned from a Business Objects Data Source?

1 Answer 999 Views
Report Designer (standalone)
David
Top achievements
Rank 1
David asked on 30 Jan 2023, 02:53 PM
Hello, I have a business objects data source with a method that returns a JSON string. Is there a way to add this data object to the report designer such that it deserializes the JSON string to determine the available properties? I tried adding a JSON data source but it looks like the only source options are an external file or inline json text.

1 Answer, 1 is accepted

Sort by
0
Momchil
Telerik team
answered on 02 Feb 2023, 01:37 PM

Hi David,

Assuming that you have a configured ObjectDataSource, I can suggest one of the following approaches that will allow you to use the JSON result from your data source.

Approach 1:

1) Add a JsonDataSource to your report and name it.

(Optional) You might want to put a sample JSON result from your object data source in the inline option, in order to be able to see the different fields during design time. 

2) Set the Source of the JsonDataSource to the result of your business object programmatically. Depending on your report definition type, you can do that in one of the following ways.

public class MyReportSourceResolver : IReportSourceResolver
{
        public string ReportsPath { get; set; }
        public MyReportSourceResolver(string reportsPath)
        {
            this.ReportsPath = reportsPath;
        }
        public ReportSource Resolve(string reportId, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
        {
            string reportPath = Path.Combine(this.ReportsPath, reportId);
            var reportPackager = new ReportPackager();
            Report report = null;
            using (var sourceStream = System.IO.File.OpenRead(reportPath))
            {
                report = (Report)reportPackager.UnpackageDocument(sourceStream);
            }
            if (operationOrigin == OperationOrigin.GenerateReportDocument)
            {
                var jsonDataSources = report.GetDataSources().OfType<JsonDataSource>();
                foreach (var jsonDataSource in jsonDataSources)
                {
                    if(jsonDataSource.Name == "JsonDataSourceName")
                    {
                        jsonDataSource.Source = "business object result";
                    }
                }
            };
            return new InstanceReportSource
            {
                ReportDocument = report
            };
        }
}

Note that the code highlighted in blue in the above snippet shows how you can unpackage a TRDP or a TRBP report definition. For reference on how to open a TRDX definition, refer to the Serialize Report Definition in XML documentation article.

Approach 2:

1) This step is the same as step 1 in the previous section.

2) Modify your existing ObjectDataSource, so that it returns the JSON string as a data source field if it does not do so already.

3) Set JsonDataSource you created in step 1 to the report item you want to use it for, and modify its Source through a Binding, as described in the Setting the content of JsonDataSource through report parameter KB article.

You do not need to use a parameter in this case. For example, if your existing data source is set to the report, and you want to use the JSON in a table, you can apply the following binding to the table:

Property path: DataSource.Source
Expression: =Fields.MyJsonResult

Another relevant article you might find useful:

How to Use Collection Properties as DataSources - Telerik Reporting

 

Best Regards,
Momchil
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Tags
Report Designer (standalone)
Asked by
David
Top achievements
Rank 1
Answers by
Momchil
Telerik team
Share this question
or