# Nested Datasets

Learn how to create a Page report using a nested JSON dataset. Understand how to leverage hierarchical data structures in your reports.

## Content

A nested dataset represents JSON or XML data as an hierarchical structure. A nested JSON or XML dataset is most commonly used in a bound data region, where you can partially use the dataset nodes for different data regions of your report.

Follow these steps to create a report with a JSON nested dataset that contains data on Movies and the movie information on Roles, created from attached file:
[JSON With Nested Data.json](https://cdn.mescius.io/document-site-files/attachments/b298aea2-8cb8-4a0f-be88-4f54aa263ebd/JSON%20With%20Nested%20Data.f4e6a4.json)
![Report with Nested Dataset at Preview](https://cdn.mescius.io/document-site-files/images/b298aea2-8cb8-4a0f-be88-4f54aa263ebd/images/nested-dataset-developer.png)

1. In Visual Studio 2022, create a new Windows Forms project.
2. Right-click the project in the Solution Explorer and select **Manage NuGet Packages for Solution**.
3. On the **Browse** tab of the opened **NuGet - Solution** window, search and install the **MESCIUS.ActiveReports** and **MESCIUS.ActiveReports.Design.Win** NuGet packages.
4. On the Form.cs, double-click the title bar to create the Form1\_Load event.
5. At the top of the Form1 code view, add these using directives.

    ```csharp
    using GrapeCity.ActiveReports.Design.ReportExplorer;
    using GrapeCity.ActiveReports.Design;
    using GrapeCity.ActiveReports.PageReportModel;
    using GrapeCity.ActiveReports;
    using System.Xml;
    using TextBox = GrapeCity.ActiveReports.PageReportModel.TextBox;
    using DataSet = GrapeCity.ActiveReports.PageReportModel.DataSet;
    using DataSource = GrapeCity.ActiveReports.PageReportModel.DataSource;
    using Field = GrapeCity.ActiveReports.PageReportModel.Field;
    ```
6. In the existing code, add the following code.

    ```csharp
    namespace NestedDataSetSample
    {
        public partial class Form1 : Form
        {
            Designer designer;
            PropertyGrid propertyGrid;
            ReportExplorer reportExplorer;
            PageReport report;
            ReportSection section;
            public Form1()
            {
                InitializeComponent();
            }
    ```
7. Add the following code to the Form1\_Load event.

    ```csharp
    {
        CreateLayout();
        CreateReport();
        CreateDataRegions();
        CreateDataSource();
        designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
    }
    ```
8. In the existing code, add the following code to create the Designer, PropertyGrid and ReportExplorer objects.

    ```csharp
    private void CreateLayout()
    {
        designer = new Designer() { Dock = DockStyle.Fill };
        propertyGrid = new PropertyGrid() { Dock = DockStyle.Right };
        designer.PropertyGrid = propertyGrid;
        reportExplorer = new ReportExplorer()
        {
            Dock = DockStyle.Left,
            ReportDesigner = designer
        };
        Controls.Add(designer);
        Controls.Add(propertyGrid);
        Controls.Add(reportExplorer);
    }
    ```
9. Add the following code to create the PageReport and ReportSection objects.

    ```csharp
    private void CreateReport()
    {
         report = new PageReport();
         section = new ReportSection();
         section.Name = "Section1";
         section.Width = "15cm";
         section.Body.Height = "20cm";
         report.Report.ReportSections.Add(section);
    }
    ```
10. Add the following code to create data regions that will contain report data.

```auto
```csharp
private void CreateDataRegions()
{
    List listMovies = new List()
    {
        Name = "List_Movies",
        Top = "0.8cm",
        Left = "0.6cm",
        Width = "11cm",
        Height = "5cm",
        Style = new Style() { FontFamily = "Arial" },
        DataSetName = "Movies",
        ZIndex = 1
    };
    listMovies.ReportItems.Add(new TextBox()
    {
        Name = "TextBox_Title",
        Top = "0.6cm",
        Left = "1cm",
        Width = "2.5cm",
        Height = "0.75cm",
        Style = new Style()
        {
            FontFamily = "Arial",
            PaddingBottom = "2pt",
            PaddingLeft = "2pt",
            PaddingRight = "2pt",
            PaddingTop = "2pt"
        },
        Value = "=Fields!Title.Value",
        DataElementName = "Title",
        ZIndex = 1
    });
    List listCast = new List()
    {
        Name = "List_Cast",
        Top = "1.8cm",
        Left = "1.4cm",
        Width = "5cm",
        Height = "2.2cm",
        Style = new Style() { FontFamily = "Arial" },
        ZIndex = 2,
        DataSetName = "Cast"
    };
    listCast.ReportItems.Add(new TextBox()
    {
        Name = "TextBox_Name",
        Top = "0.6cm",
        Left = "0.6cm",
        Width = "2.5cm",
        Height = "0.75cm",
        Style = new Style()
        {
            FontFamily = "Arial",
            PaddingBottom = "2pt",
            PaddingLeft = "2pt",
            PaddingRight = "2pt",
            PaddingTop = "2pt"
        },
        DataElementName = "Name",
        Value = "=Fields!Name.Value",
    });
    listMovies.ReportItems.Add(listCast);
    section.Body.ReportItems.Add(listMovies);
}
```

```auto

11. Add the following code to create a JSON data source, add a dataset and a nested dataset, and add fields to both datasets.

```auto
```csharp
private void CreateDataSource()
{
    DataSource dataSource = new DataSource();
    dataSource.Name = "JSONDataSource";
    dataSource.ConnectionProperties = new ConnectionProperties()
    {
        DataProvider = "JSON",
        ConnectString = "jsondoc={path to file}\\JSON With Nested Data.json"
    };
    report.Report.DataSources.Add(dataSource);
    report.Report.DataSets.Add(CreateDataSet(dataSource));
    report.Report.DataSets.Add(CreateSecondDataSet());
}
private DataSet CreateDataSet(DataSource _DataSource)
{
    Query query = new Query
    {
        Timeout = 30,
        CommandText = "$.Movie[*]",
        DataSourceName = _DataSource.Name,
    };
    DataSet dataSet = new DataSet
    {
        Query = query,
        Name = "Movies",
    };
    CreateFieldsToDataSet(dataSet);
    return dataSet;
}
private DataSet CreateSecondDataSet()
{
    Query query = new Query
    {
        Timeout = 30,
        CommandText = "$",
        DataSourceName = "$dataset:Movies/Cast",
    };
    DataSet dataSet = new DataSet
    {
        Query = query,
        Name = "Cast",
    };
    CreateFieldsToSecondDataSet(dataSet);
    return dataSet;
}
private void CreateFieldsToDataSet(DataSet dataSet)
{
    Field field = new Field
    {
        DataField = "Title",
        Name = "Title"
    };
    dataSet.Fields.Add(field);
}
private void CreateFieldsToSecondDataSet(DataSet dataSet)
{
    Field field = new Field
    {
        DataField = "Name",
        Name = "Name"
    };
    dataSet.Fields.Add(field);
}
```

```auto

12. Add the following code to load the report to the Designer in Form1\_Load event.

```auto
```csharp
designer.LoadReport(XmlReader.Create(new StringReader(report.ToRdlString())), DesignerReportType.Page);
```

```auto

13. Run the project and preview the report.

## See Also

[Quick Start](/activereportsnet/docs/developers/quick-start)

#### Report Authors

[Nested Datasets (JSON and XML)](/activereportsnet/docs/report-authors/data-binding/data-binding-page-rdl-reports/add-dataset/nested-datasets-json-xml)
```