Document Solutions for Word
Report Templates / Walkthrough / Generate Document using JSON Data Source
In This Topic
    Generate Document using JSON Data Source
    In This Topic

    Report Templates allow you to bind the template with JSON data source by using Add method of IDictionary interface. The data can be added from a JSON file, stream, string etc. and bound to populate actual data upon template processing. For more information on different ways to work with JSON data source, refer Data Source Binding.

    The following example takes you through a step by step process to generate a Word document with the list of oceans provided as a JSON string. 

    1.  Create a template layout in a Word document as shown below. You can also download the Template layout from here.

    2. Load the template created in above step, in DsWord.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      //Load template layout
          doc.Load("OceansList[Template].docx");

    3. Pass the data in a JSON string.
      C#
      Copy Code
      var oceans = @"[
              {
                  ""name"": ""Pacific"", 
                  ""areaOfWorldOcean"": 0.466, 
                  ""volumeOfWorldOcean"": 0.501, 
                  ""seas"": [
                      {""name"": ""Australasian Mediterranean Sea""}, 
                      {""name"": ""Philippine Sea""}, 
                      {""name"": ""Coral Sea""}, 
                      {""name"": ""South China Sea""}],
              },
              {
                  ""name"": ""Atlantic"", 
                  ""areaOfWorldOcean"": 0.235, 
                  ""volumeOfWorldOcean"": 0.233, 
                  ""seas"": [
                      {""name"": ""Sargasso Sea""}, 
                      {""name"": ""Caribbean Sea""}, 
                      {""name"": ""Mediterranean Sea""}, 
                      {""name"": ""Gulf of Guinea""}],
              },
              {
                  ""name"": ""Indian"", 
                  ""areaOfWorldOcean"": 0.195, 
                  ""volumeOfWorldOcean"": 0.198, 
                  ""seas"": [
                      {""name"": ""Arabian Sea""}, 
                      {""name"": ""Bay of Bengal""}, 
                      {""name"": ""Andaman Sea""}, 
                      {""name"": ""Laccadive Sea""}],
              },
              {
                  ""name"": ""Southern"", 
                  ""areaOfWorldOcean"": 0.061, 
                  ""volumeOfWorldOcean"": 0.054, 
                  ""seas"": [
                      {""name"": ""Weddell Sea""}, 
                      {""name"": ""Somov Sea""}, 
                      {""name"": ""Riiser-Larsen Sea""}, 
                      {""name"": ""Lazarev Sea""}],
              },
              {
                  ""name"": ""Arctic"", 
                  ""areaOfWorldOcean"": 0.043, 
                  ""volumeOfWorldOcean"": 0.014, 
                  ""seas"": [
                      {""name"": ""Barents Sea""}, 
                      {""name"": ""Greenland Sea""}, 
                      {""name"": ""East Siberian Sea""}, 
                      {""name"": ""Kara Sea""}],
              }
          ]";

    4. Add above data as a data source for the template.
      C#
      Copy Code
      doc.DataTemplate.DataSources.Add("ds", oceans);

    5. Process the template by using Process method.
      C#
      Copy Code
      //Process the template
      doc.DataTemplate.Process();

    6. Save the final Word document.
      C#
      Copy Code
      //Save the final document
      doc.Save("List of Oceans.docx");

      The output of the above code is shown as below: