JsonDataSource foreach empty list throw exception

Aspose-Words version :24.3

JsonDataSource

{
  "BM01": {
    "Table_FS_CAC_CUR": []
  }
}

Template

<<foreach [tableData in BM01.Table_FS_CAC_CUR]>>
<<[inname]>>
<</foreach>>

Code

ReportingEngine engine = new ReportingEngine();
JsonDataLoadOptions opts = new JsonDataLoadOptions();
opts.setAlwaysGenerateRootObject(true);
JsonDataSource ds = new JsonDataSource(jsonPath, opts);
engine.buildReport(doc, ds);

Exception

Can not get the value of member 'inname' on type 'class com.aspose.words.net.System.Data.DataRow'

I tried use json datasoure to build report, there are some empty lists in the json, when i built report, it throwed exception like ”Missing Member“.But the list count of the key is zero,I think it should not be foreached.

I tried a new template like this,but also throwed the exception.

<<if [BM01.Table_FS_CAC_CUR.count()>0]>>

<<foreach [tableData in BM01.Table_FS_CAC_CUR]>>
<<[tableData.inname]>>
<</foreach>>

<</if>>

@Doraemon You should specify ReportBuildOptions.ALLOW_MISSING_MEMBERS. Please see our documentation to learn how to deal with missed members.

@alexey.noskov
I need the missing member error,so I can’t use ReportBuildOptions.ALLOW_MISSING_MEMBERS.

I think it’s a bug, as stated in Working with Common Conditional Blocks in Java|Aspose.Words for Java ,the count() or any() in <<if>> should work fine

@Doraemon LINQ Reporting Engine parses a template document as a whole before evaluating any expression. The engine does not skip parsing of expressions, which are not going to be evaluated according to template logic and data. In this case, there is no member inname defined for tableData items in your JSON file, hence the parsing error appears, which is an expected behavior rather than a bug.

ReportBuildOptions.ALLOW_MISSING_MEMBERS option designed for scenarios like yours.

@alexey.noskov
I got it.

But in my json, there are both empty lists and not empty lists.I want to check members in not empty lists and not check memebers in empty lists. How can i do it?

JSON like this:

{
    "BM01": {
//not check members
        "emptyList": [],

//check members
        "notEmptyList": [
            {
                "someMember": "someValue"
            }
        ]
    }
}

@Doraemon You can check whether the list is empty, but this will not avoid checking syntax inside <<if>> condition. This is the same if in your code you try to access property or method of an object that does not have such property or method. Compiler will not compile such code.

@alexey.noskov
Does linq report engine support groovy class? If i use groovy instance as datasource, how the engine access the properties? The groovy class can have dynamic meta proerties.

@Doraemon Please see our documentation to learn more about working with types:
https://docs.aspose.com/words/java/working-with-types/
https://docs.aspose.com/words/java/accessing-type-members/

@alexey.noskov
Thanks for your reply. I got it.

1 Like