JsonDataSources in LINQ engine not resolving

I have read through the LINQ syntax examples, but whatever I try is not resolving for JsonDataSource. Here is the code:

try
{
    ReportingEngine engine = new ReportingEngine();
    HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.HTML, "", "");
    String htmlTemplate = "<<[Employee.NameFirst]>> <<[Employee.NameLast]>>";
    InputStream targetStream = new ByteArrayInputStream(htmlTemplate.getBytes());
    Document doc = new Document(targetStream, loadOptions);

    List<Object> dataSources = new ArrayList<Object>();
    List<String> dataSourceNames = new ArrayList<String>();

    String jdsJsonEmployee = "{ \"Employee\": { \"EmployeeID\": 34153, \"NameFirst\": \"John\", \"NameLast\": \"Smith\" } }";
    InputStream dsStreamEmployee = new ByteArrayInputStream(jdsJsonEmployee.getBytes());
    JsonDataLoadOptions options = new JsonDataLoadOptions();
    options.setAlwaysGenerateRootObject(false); //tried this true and false, same error
    JsonDataSource jdsEmployee = new JsonDataSource(dsStreamEmployee, options);
    dataSources.add(jdsEmployee);
    dataSourceNames.add("Employee");

    boolean result = engine.buildReport(doc, dataSources.toArray(), Arrays.toString(dataSourceNames.toArray()));
    // this call to ReportingEngine.buildReport() results in exception:
    // IllegalStateException: An error has been encountered at the end of expression 'Employee.NameFirst]>'. 
    // Can not get the value of member 'Employee' on type 'class [Ljava.lang.Object;'. 

    if (result)
    {
        HtmlSaveOptions saveOptions = new HtmlSaveOptions();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        doc.save(outputStream, saveOptions);

        String beforeDecoding = outputStream.toString(Charset.defaultCharset().toString());
        String retVal = StringEscapeUtils.unescapeHtml(StringEscapeUtils.unescapeJava(beforeDecoding));
        LogHelper.logs().log("Report Html Result=" + retVal);
        return retVal;
    }
}
catch (Exception e)
{
    LogHelper.logs().log(e);
    throw new InternalErrorException(e);
}

And here is the exception:

IllegalStateException: An error has been encountered at the end of expression 'Employee.NameFirst]>'. Can not get the value of member 'Employee' on type 'class [Ljava.lang.Object;'. = > java.lang.IllegalStateException: An error has been encountered at the end of expression 'Employee.NameFirst]>'. Can not get the value of member 'Employee' on type 'class [Ljava.lang.Object;'.
  at com.aspose.words.internal.zzXzc.zzwE(Unknown Source)
  at com.aspose.words.internal.zzXzc.zzwE(Unknown Source)
  at com.aspose.words.internal.zzns.zzVP7(Unknown Source)
  at com.aspose.words.internal.zzZua.zzWWr(Unknown Source)
  at com.aspose.words.internal.zzZua.zzXmb(Unknown Source)
  at com.aspose.words.internal.zzZua.zzWMB(Unknown Source)
  at com.aspose.words.internal.zzns.zzwE(Unknown Source)
  at com.aspose.words.internal.zzns.zzwE(Unknown Source)
  at com.aspose.words.internal.zziP.zzwE(Unknown Source)
  at com.aspose.words.internal.zziP.zzwE(Unknown Source)
  at com.aspose.words.ReportingEngine.buildReport(Unknown Source)
  at com.aspose.words.ReportingEngine.buildReport(Unknown Source) 	

@rocketpower The problem is that Arrays.toString(dataSourceNames.toArray()) returns string representation of the array, but not an array of strings in the passed array. Please modify your code like this:

boolean result = engine.buildReport(doc, dataSources.toArray(), dataSourceNames.toArray(new String[0]));

Or use the following code;

boolean result = engine.buildReport(doc, jdsEmployee, "Employee");

Thank you. Case closed.

1 Like