Trouble in Displaying a List of String

I trying to display a list of string, my Json input is

{
  "NAMES":["NAME1","NAME2","NAME3"]
}

and I’m initializing the JsonDataSource as strict

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.SimpleValueParseMode = JsonSimpleValueParseMode.Strict;

var dataSource = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(input.ToString())), options);
engine.BuildReport(document, dataSource);

In Template, I have the Syntax
<<foreach[item in NAMES]>><<[item]>><</foreach>>

I’m getting a response as
System.Data.DataRowSystem.Data.DataRowSystem.Data.DataRow

@PramodMn

It appears you are facing an issue with displaying a list of strings from your JSON input in your template. The problem seems to stem from the syntax used in the template, which is not correctly accessing the string values.

In your template, you currently have:

<<foreach[item in NAMES]>><<[item]>><</foreach>>

The correct syntax to access the items in the list should be:

<<foreach item in NAMES>> <<item>> <</foreach>>

Make sure to remove the extra brackets around item. This adjustment will allow you to correctly iterate over the NAMES array and display each name.

If you continue to experience issues, please verify that your JSON input is correctly formatted and that the data source is properly initialized.

Here’s the corrected version of your template syntax again for clarity:

<<foreach item in NAMES>> <<item>> <</foreach>>

This should resolve the issue and display the names as expected. If you have any further questions or need additional assistance, feel free to ask!

is not a valid syntax, it throws exception.
Unhandled exception. System.InvalidOperationException: Tag ‘item’ is not well-formed. Tag ‘item’ must be located within a title of a combobox or dropdown list content control.

@PramodMn You should use the following syntax in your template:

<<foreach [in NAMES]>><<[NAMES_Text]>><</foreach>>

And specify JsonDataLoadOptions.AlwaysGenerateRootObject:

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.SimpleValueParseMode = JsonSimpleValueParseMode.Strict;
options.AlwaysGenerateRootObject = true;

JsonDataSource dataSource = new JsonDataSource(@"C:\Temp\data.json", options);

Document doc = new Document(@"C:\Temp\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dataSource);

doc.Save(@"C:\Temp\out.docx");

Please see our documentation for more information:
https://docs.aspose.com/words/net/accessing-json-data/

is adding this necessary, if my json model is very simple, root node and one level inside
modifying the template with this logic isn’t sufficient?
<<foreach [in NAMES]>><<[NAMES_Text]>><</foreach>>

@PramodMn On my side the test scenario does not work without AlwaysGenerateRootObject option.

ok… Thanks, I tried without AlwaysGenerateRootObject option , just updating Template and it seems working… My Json Model is just a Dictionary<string,object>, where the Value Object can be string, number, or a list of strings

1 Like