Convert String into JSON Data source using Java

Hi,

I know I can use JSON datasources upfront but now I have a different use case. We pass a Java object Order which has a method getOrderLines. Each OrderLine instance has a method getPropertiesAsJson which returns a stringified JSON array, e.g. "[{\"propertyName\":\"color\", \"propertyValue\":\"red\"},{\"propertyName\":\"width\", \"propertyValue\":\"125 mm\"}]" with properties specific for that order line.

Can we convert that stringified JSON to a JSON datasource so we can use a construct like this:

<<foreach [line in order.getOrderLines()]>>
<<foreach [property in line.getPropertiesAsJson()]>>
...
<</foreach>><</foreach>>

We now receive an error that Aspose can’t iterate over an object of type java.lang.Object.

@bottemav

Aspose.Words does not provide API to convert Java object to JSON data source. We suggest you please read following article.

LINQ Reporting Engine API

Could you please complete detail of your requirement? Please also share why you want to convert Java object to JSON. You can use Java object as data source for LINQ Reporting. Please refer to the following article.
Hello World Example

@tahir.manzoor Each tenant in our Java program can store their specific details of an order line as an array of JSON objects in a property of type java.lang.String. The structure of the JSON objects differs from tenant to tenant, so we can’t convert the JSON objects to Java objects. The JSON objects can also be nested, e.g. "[{\"propertyName\":\"color\", \"propertyValue\":\"red\", \"certificates\": [{\"name\":\cert1\"}]}]"
When we create a Word document for the tenant we want to be able to iterate over the properties.

An alternative to converting the java.lang.String to a JSON datasource while executing the report is to add a JSON datasource for each order line with a distinct name, e.g. propertiesDs-orderLineId1, propertiesDs-orderLineId2 but then we should be able to lookup the right datasource based on the ID of the order line. And I think this isn’t possible either.

<<foreach [line in order.getOrderLines()]>>
<<foreach [property in 'propertiesDs-' + line.getId()]>>
...
<</foreach>><</foreach>>

I hope this clarifies our use case.

@bottemav

To generate the report using LINQ Reporting engine, you need to use the correct data source as explained in the article shared in my previous post.

Aspose.Words does not provide APIs to convert such strings to JSON.

Could you please attach the following resources here for our reference? Perhaps, there is some other way to achieve your requirement.

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to export the document.

Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor

I composed a test case on Java 8: asposeJsonToDatasource.zip (24.5 KB)
The Word template is located in the folder src\main\resources.
The expected output Word file is itemJsonExpectedResult.docx.
You can see the error message I get now in the file currentOutput.txt

@bottemav

Thanks for sharing the detail. We are investigating your issue and will get back to you soon.

@bottemav

Thanks for sharing the detail and patience.

[{“propertyName”:“processor”, “propertyValue”:“Intel Core i5”},{“propertyName”:“memory”, “propertyValue”:“16 GB”}]

<<foreach [property in o]>>
    <<[ property.propertyName]>> : <<[ property.propertyValue]>>
<</foreach>>

Following code example shows how to use the JsonDataSource with above template. You can use the same approach to convert string to JSON and use it in your code. Hope this helps you.

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.setSimpleValueParseMode(JsonSimpleValueParseMode.LOOSE);

JsonDataSource dataSource = new JsonDataSource(MyDir + "in.json", options);

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

ReportingEngine engine = getReportingEngine();
engine.buildReport(doc, dataSource, "o");
doc.save(MyDir + "21.1.docx", SaveFormat.DOCX);

Hi @tahir.manzoor
This does not help because I have multiple order lines with a JSON. If I create a JsonDataSource for every order line, how to reference them in the document?

<<foreach [line in order.getOrderLines()]>>
<<foreach [property in ??]>>

<>
<>

@bottemav

We have logged your requirement as WORDSNET-21804 in our issue tracking system. We will check the possibility of implementation of this new feature and inform you via this forum thread once there is an update available on it. We apologize for your inconvenience.