I have an issue with outputting sequential data if the field that should be iterated has a null value in the JSON.
JSON:
{
"data":{
"contact":{
"array": null
}
}
}
In the document I have the next data:
<<foreach [item in contact.array]>>
<<[item.firstName]>>
<<[item.lastName]>>
<</foreach>>
Here is the exception that I have with the example above:
Exception in thread “main” java.lang.IllegalStateException: An error has been encountered at the end of the expression ‘item in contact.array]>’. Can not iterate over an instance of type ‘class java.lang.String’.
I tried to check the contact.array for null and empty string in the following way
<<if contact.array != null && contact.array != “”>>
As a result, the same error
I’m using the 21.12 version of Aspose.Words
@bz.anton.kh
The issue you’re encountering with the LINQ Reporting Engine is due to attempting to iterate over a null value in your JSON. When contact.array
is null, the engine cannot perform the iteration, leading to the exception you’re seeing.
To handle this situation, you can modify your template to check if contact.array
is not null before attempting to iterate over it. Here’s how you can adjust your template:
<<if contact.array != null>>
<<foreach [item in contact.array]>>
<<[item.firstName]>>
<<[item.lastName]>>
<</foreach>>
<</if>>
This way, the iteration will only occur if contact.array
is not null, preventing the exception from being thrown.
If you continue to experience issues, ensure that your JSON structure is correctly formatted and that the LINQ Reporting Engine is properly set up to handle the data types you’re working with.
I tried to add an if, but it didn’t help. I attached java class with code, document and json.
support.zip (11.1 KB)
@bz.anton.kh
Please try using ReportBuildOptions.ALLOW_MISSING_MEMBERS
like that:
ReportingEngine engine = new ReportingEngine();
engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS);
engine.buildReport(...);
See Accessing Missing Members of Data Objects for more information.
I enabled this option, but the result is the same
In addition to the previous guidance, please try to apply JsonSimpleValueParseMode.STRICT
as follows:
JsonDataLoadOptions options = new JsonDataLoadOptions();
options.setSimpleValueParseMode(JsonSimpleValueParseMode.STRICT);
JsonDataSource dataSource = new JsonDataSource(..., options);
I mean using of ReportBuildOptions.ALLOW_MISSING_MEMBERS
and JsonSimpleValueParseMode.STRICT
together. Please also note that if
s in the template should be removed.
The error is still the same
Exception in thread "main" java.lang.IllegalStateException: An error has been encountered at the end of expression 'item in contact.array]>'. Can not iterate over an instance of type 'class java.lang.String'.
support.zip (11.1 KB)
@bz.anton.kh
In your code, ReportBuildOptions.ALLOW_MISSING_MEMBERS
is not applied and there is the following line:
engine.setOptions(ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS);
It should be as follows instead:
engine.setOptions(ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS | ReportBuildOptions.ALLOW_MISSING_MEMBERS);
I added this option, unfortunately it didn’t help.
Exception in thread "main" java.lang.IllegalStateException: An error has been encountered at the end of expression 'item in contact.array]>'. Can not iterate over an instance of type 'class java.lang.String'.
It looks like Aspose converts the null field to a String
@bz.anton.kh
I was able to reproduce the issue using older versions of Aspose.Words for Java. However, for example, using Aspose.Words for Java 23.10, the issue is not reproducible anymore. So it seems that you may need to upgrade Aspose.Words for Java version.
Thanks a lot, have one another issue with parsing strict mode with next JSON:
{
"data":{
"dynamicFields": {},
"createdDate": "2024-08-15T08:14:14Z",
"contact":{
"array": null
}
}
}
Exception:
Exception in thread "main" java.lang.NullPointerException: Cannot load from int array because "<parameter3>" is null
@bz.anton.kh
We were able to reproduce the issue on our end. For the sake of further analysis and possible correction, we have opened the following new ticket(s) in our internal issue tracking system and will work on them according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28690
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.