JSON Content and Word Output
The JSON content is: {"form":{ "gender":"M"}}
.
In Word, the output statement is: <<[form.gender]>>
, which works fine.
However, if the JSON content is: {"form":{ "smoke":["Y","N"]}}
.
In Word, the output statement <<[form.smoke#0]>>
results in an error.
How do I correctly output the data at index 0 of the JSON array?
@chongpeng
To correctly output the data at index 0 of a JSON array in your Word template, you should use the “_Text” suffix in your template syntax. For your specific case, where the JSON content is {"form":{ "smoke":["Y","N"]}}
, you can reference the first element of the “smoke” array using the following syntax:
<<[form.smoke_Text]>>
This approach allows you to access the value of the first item in the “smoke” array without encountering errors. The “_Text” suffix is necessary when dealing with properties that are arrays of simple-type values in your JSON data.
For further details on accessing JSON data in Aspose.Words, you can refer to the documentation on accessing JSON data, which explains how to work with both simple and complex JSON types in template documents.
If you have any more questions or need further assistance, feel free to ask!
Sources:
[1]: Accessing JSON Data in C#|Aspose.Words for .NET
1 Like
@chongpeng
LINQ Reporting Engine uses a subset of C# language in its template syntax and supports working with collections in the form of LINQ extension methods, so the provided syntax is not valid and hence, raises an error. Also, working with JSON arrays of simple types requires special treatment as per Accessing JSON Data. Combining these all together, valid syntax in this case would be <<[form.smoke.ElementAt(0).smoke_Text]>>
.
1 Like