elementAt(index) is not working in "foreach" tag in Aspose Document Template

Hello Team,

We are working on Aspose LINQ Document Template which uses the .docx as input template & generates a PDF based on the JSON data.

But we are facing an issue while iterating 2 JSON array data in a table using the Aspose syntax in the word template.

Input:

"JsonArray1": [
   {
    "Name": "France"
   },
   {
    "Name": "Italy"
   },
   {
    "Name": "Supranational"
   }
]

"JsonArray2": [
   {
    "Name": "Belgium"
   },
   {
    "Name": "United Kingdom"
   },
   {
    "Name": "Austria"
   }
]

Try 1:

Column1 Column2 Column3 Column4
<<foreach [item in JsonArray1]>> <<if [item.indexOf() < 15]>><<[item.Name]>><</if>><</foreach>> <<foreach [item in JsonArray2]>> <<if [item.indexOf() < 15]>><<[item.Name]>><</if>><</foreach>>

Try 2: (Using JsonArray1’s index in elementAt() to get value from JsonArray2 in JsonArray1’s iteration)

Column1 Column2 Column3
<<foreach [item in JsonArray1]>> <<if [item.indexOf() < 15]>><<[item.Name]>> <<[JsonArray2.elementAt(item.indexOf()).Name]>><</if>><</foreach>>

Try 3: (Hard coded the index in elementAt() in JsonArray1’s iteration)

Column1 Column2 Column3
<<foreach [item in JsonArray1]>> <<if [item.indexOf() < 15]>><<[item.Name]>> <<[JsonArray2.elementAt(0).Name]>><</if>><</foreach>>

Above all 3 tries are not working.

But JsonArray2.elementAt(0).Name is working fine outside of foreach.

Any idea to resolve this issue / scenario ?

Thanks in advance…!

@velmurugan.veerarajan You should use a root object in your code. Please try modifying your Json like this:

{
  "Data": {
    "JsonArray1": [
      {
        "Name": "France"
      },
      {
        "Name": "Italy"
      },
      {
        "Name": "Supranational"
      }
    ],

    "JsonArray2": [
      {
        "Name": "Belgium"
      },
      {
        "Name": "United Kingdom"
      },
      {
        "Name": "Austria"
      }
    ]
  }
}

here is test code, input and output documents:

JsonDataLoadOptions opt = new JsonDataLoadOptions();
opt.setAlwaysGenerateRootObject(true);
JsonDataSource data = new JsonDataSource("C:\\Temp\\data.json", opt);
        
Document doc = new Document("C:\\temp\\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, data);
doc.save("C:\\Temp\\out.docx");

in.docx (12.9 KB)
out.docx (10.3 KB)