I want to access an array element by index through Aspose.Word Linq Reporting.
i tried the following ways, but it won’t work.
<<[formData.ancillaryActivities.withinTheFirm.first().entityDescription]>>
<<[formData.ancillaryActivities.withinTheFirm.elementAt(0).entityDescription]>>
Payload
{
"profile": {},
"formData": {
"firmCensusProfileInfo": {
"mainFirmName": "TestFirm",
"firmLocation": "USA"
},
"ancillaryActivities": {
"withinTheFirm": [
{
"entityDescription": "Entity1",
"entityType": "Broker"
},
{
"entityDescription": "Entity2",
"entityType": "UW"
},
{
"entityDescription": "Entity3",
"entityType": "Employee"
}
]
}
},
"Addl": {}
}
@lalit2207
To access an array element by index in Aspose.Words LINQ Reporting, you need to follow a specific syntax due to how JSON data is treated in this context.
When dealing with JSON arrays, you can use the First()
method to access the first element, but for accessing specific elements by index, you should use the property name with a _Text
suffix.
For your case, to access the entityDescription
of the first element in the withinTheFirm
array, you can use the following syntax:
<<[formData.ancillaryActivities.withinTheFirm.first().entityDescription_Text]>>
If you want to access the second element, you would use:
<<[formData.ancillaryActivities.withinTheFirm.elementAt(1).entityDescription_Text]>>
This approach allows you to reference the properties of the objects within the array correctly. Remember that LINQ operations like First()
are available, but direct indexing with elementAt()
may not work as expected due to the way JSON data is processed in Aspose.Words LINQ Reporting.
For more detailed information, you can refer to the documentation on accessing JSON data in Aspose.Words here.
If you have any further questions or need additional examples, feel free to ask!
I tried both the ways , but it doint help,
<<[formData.ancillaryActivities.withinTheFirm.first().entityDescription_Text]>>
<<[formData.ancillaryActivities.withinTheFirm.elementAt(1).entityDescription_Text]>>
I am attaching my template
ForeachTemplate.docx (15.4 KB)
@lalit2207 You should use the following syntax:
<<[formData.ancillaryActivities.withinTheFirm.ElementAt(1).entityDescription]>>
Here is the modified template and test code:
JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json", new JsonDataLoadOptions() { AlwaysGenerateRootObject = true });
Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds);
doc.Save(@"C:\Temp\out.docx");
data.zip (361 Bytes)
in.docx (15.5 KB)
out.docx (12.2 KB)