How to use JSON Data Source with Same Table Name for Parent and Child using Java | LINQ Reporting Engine

I’m evaluating Words for Java and using version 21.7. I’m attempting to use JSON data and encountering a number of issues.

My code is quite simple at the moment:

@Configuration
class CliRunner(private val resources: Resources) : CommandLineRunner {
    override fun run(vararg args: String?) {
        val jsonOptions = JsonDataLoadOptions()

        val dataSource = JsonDataSource(resources.hierarchyData().inputStream, jsonOptions)
        val template = resources.hierarchyTemplate().inputStream

        val document = Document(template)

        val engine = ReportingEngine()
        engine.options = ReportBuildOptions.ALLOW_MISSING_MEMBERS + ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS;


        engine.buildReport(document, dataSource)
        document.save(outputFile().outputStream(), SaveFormat.PDF)
    }

    private fun outputFile(): File {
        val desktop = "${System.getProperty("user.home")}/Desktop"

        return File("$desktop/result.pdf")
    }
}

Problem #1:
I have the following JSON:

{
 "nodes": [
   {
    "id": "1.0"
   },
   {
     "id": "2.0"
   }
 ]
}

And the following Word template content:

<<foreach [n in nodes]>>
Node ID: <<[n.id]>>
<</foreach>>

I would expect to see both node IDs rendered in the PDF document; however, the content is blank. When I add an additional, non-array based value then everything works as expected - for example "anything": 1 or "anything": {}.

Similarly, if I have the the following JSON:

{
  "data": {
    "id": "data.id value"
  }
}

And the following template:

<<[data?.id]>>

I would expect to see “data.id value” rendered; however, I see nothing. If I add another key, it works fine (example "id": "root.id value").

Is this a known issue or a limitation of the JsonDataSource?

Problem #2:
I’m noticing nested JSON keys of the same name behave in unexpected ways when the nest keys’ value are objects or arrays.

Given the following JSON and template:

{
  "data": {
    "id": "data.id value",
    "data": "data.data value"
  },
  "id": "root.id value"
}

and

<<[id]>>
<<[data?.id]>>
<<[data?.data]>>

Everything works fine here; however, if I change “data.data” to an object or array then the data is not rendered.

With this JSON and template

{
  "data": {
    "id": "data.id value",
    "data": {
      "id": "data.data.id value"
    }
  },
  "id": "root.id value"
}

And

<<[id]>>
<<[data?.id]>>
<<[data?.data?.id]>>

I see nothing rendered for “data.data.id” and the same applies if “data.data” was an array (with template changes). If I disable ALLOW_MISSING_MEMBERS, then I get an exception.

Also, this doesn’t seem to be related to direct descendants/ancestors. For example, given the following JSON and template:

{
  "data": {
    "id": "data.id value",
    "anything": {
      "id": "data.anything.id",
      "data": [
        {
          "id": "data.anything.data[0].id value"
        }
      ]
    }
  },
  "id": "root.id value"
}

<<[id]>>
<<[data?.id]>>
<<[data?.anything?.id]>>
<<foreach [d in data?.anything?.data]>>
<<[d.id]>>
<</foreach>>

Nothing is rendered for the “data.anything” path (id and data paths are fine); however, if I remove “data.anything.data” and do not change the template the I see the following rendered:

root.id value
data.id value
data.anything.id
data.id value

I assume “data.id value” is rendered due to the ?.; however, that seems like a bug in the LINQ expression.

Please note that I do not intend to render hierarchy structures; however, I do need the flexibility to have key names duplicated throughout a JSON data structure given that the JSON is valid.

Are these all nasty bugs, am I missing something, and/or are nested keys of the same name unsupported by Aspose?

@raleigh.johnson

We have tested the scenario using following code examples with shared JSON. We have not found the shared issue. Please check the attached input documents.
Sample_Docs.zip (18.7 KB)

Document doc = new Document(MyDir + "template1.docx");

ReportingEngine engine = new ReportingEngine();
engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS + ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS);

JsonDataSource jsonDataSource = new JsonDataSource(MyDir + "input.json");
engine.buildReport(doc, jsonDataSource, "nodes");

doc.save(MyDir + "21.7.java.pdf");
Document doc = new Document(MyDir + "template2.docx");

ReportingEngine engine = new ReportingEngine();
engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS + ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS);

JsonDataSource jsonDataSource = new JsonDataSource(MyDir + "input2.json");
engine.buildReport(doc, jsonDataSource, "data");

doc.save(MyDir + "21.7.java.pdf");

We are checking your second issue and will get back to you soon.

@raleigh.johnson

Please change the nested ‘data’ name with another name e.g. ‘data1’ to resolve this issue.

We have logged the issue (same name of parent and child tables) in our issue tracking system as WORDSNET-22543. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Tahir,

Attached is an example that reproduces the array issue for issue #1. The sample also includes an object example; however, I’m seeing different behavior now and now see the JSON rendered as a whole.

problem-1.zip (260.8 KB)

Thanks,
Raleigh

Thanks Tahir, I look forward to hearing back about this. I’m not sure how much more I can evaluate the product without knowing if this will be resolved and when.

Regards,
Raleigh

@raleigh.johnson

Please use the data source name as shown below to avoid this issue.

engine.buildReport(doc, jsonDataSource, "nodes");
engine.buildReport(doc, jsonDataSource, "data"); 

Please change the name of parent and child tables in JSON to avoid this issue.

I had an issue in my sample were I was passing in the wrong template for the object and hence seeing different behavior. I’ve updated and see the same behavior as originally reported.

You’re correct about providing a data source name, but I don’t fully understand the difference. The documentation describes this overload as follows:

Using this overload you can reference the data source’s members and the data source object itself in the template. If you are not going to reference the data source object itself, you can omit dataSourceName passing null or use the buildReport(com.aspose.words.Document,java.lang.Object) overload.

It seems like I’m accessing the data sources members here and not the data source itself; however, its not really clear to me. Can you clarify the difference and explain why adding an additional property changes the behavior? Would XML behave differently?

Please change the name of parent and child tables in JSON to avoid this issue.

While I can change the keys to continue evaluating, it doesn’t make sense to continue my evaluation if this won’t be resolved or won’t be resolved for some time. I need to be able to use any valid JSON document regardless of the key names without special considerations. Would XML make a difference? Are there any other workarounds?

@raleigh.johnson

Please get 30 days temporary license and apply it before importing document into Aspose.Words’ DOM.

Please change the name of parent and child tables in JSON as a workaround. We will inform you via this forum thread once this issue is resolved.

We are checking this scenario and will get back to you soon.

@raleigh.johnson

We have logged this problem in our issue tracking system as WORDSNET-22554. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@raleigh.johnson

We have closed WORDSNET-22554 issue as ‘Not a bug’.

This is an expected behavior rather than a bug. Here is a relevant quote from Accessing JSON Data

By default, if a root JSON element is an object having only one property that is an object or array in turn, the engine does not generate an internal root object while loading JSON data. So, in template documents, such a JsonDataSource instance should be treated according to what this property represents instead. For instance, the following JSON snippets can be used to produce the same results in previous examples of this section respectively.

However, if your scenario requires an internal object for a root JSON element to be always generated while loading data to JsonDataSource , you can force this as shown in the following code snippet.

Please use JsonDataLoadOptions.AlwaysGenerateRootObject property as shown below to avoid the shared issue.

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.setAlwaysGenerateRootObject(true);
JsonDataSource dataSource = new JsonDataSource(..., options) 

I’m facing another issue with the JsonDataSource where I get the following error in 21.6 or 21.7; however, everything is fine in 21.5.

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

I’ve tried the nearly the same template in .NET 21.7 and 21.8 (casing changes like OrderBy vs orderBy) - both work fine.

Is this a known issue? Will a Java 21.8 build be available to try out soon?

If it helps, let me know and I can send you a sample project.

@raleigh.johnson

Please ZIP and attach your input template document and JSON data source along with code example to reproduce the same issue at our end. We will investigate the issue and provide you more information on it.

@tahir.manzoor Attached is my sample. The “assess.pdf” has data issues in 21.6/21.7; however, is fine in 21.5. I’ve tried the same in .NET with the same data/template and its fine.

aspose.zip (167.0 KB)

@raleigh.johnson

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSJAVA-2639. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSJAVA-2639) have been fixed in this Aspose.Words for Java 21.9 update.

The issues you have found earlier (filed as WORDSNET-22543) have been fixed in this Aspose.Words for Java 21.10 update.