We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Access JSON Array by Index & Bind LINQ Template to Build Report | Java

Hello Aspose Team,

Could you please help me with one question: How can I access first element in json array in Linq template with Json Data source?

E.g. I have an json object:
{
“name”: “Peter”,
“surname”: “Pan”,
“activities”: [
“walk”,
“run”
],
}

and I would like to print “walk” in linq template. I tried with <<[activities[0]]>> as normal array access, but it does not work.

Thank you.

@vova.k,

I think, you can modify JSON data source file like this:

{
  "name": "Peter",
  "surname": "Pan",
  "activities": [
    {
      "first": "walk1",
      "second": "run1"
    },
    {
      "first": "walk2",
      "second": "run2"
    }
  ]
}

And then use the following template syntax in Word document to retrieve the first values:

<<foreach [item in o.activities]>>
<<[ item.first]>> 
<</foreach>>

Java code is as follows:

Document doc = new Document("C:\\Temp\\linq test.docx");

JsonDataSource jsonDataSource = new JsonDataSource("C:\\Temp\\test.json");
Object[] sources = new Object[]{jsonDataSource};
String[] names = new String[]{"o"};

ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, sources, names);

doc.save("C:\\temp\\awjava-21.9.docx");

hallo awais,
thank you for your response.
There are two problems with the solution:
First: I have to change the structure of the input data. Do I always need to change the structure of the JSON Object in order to access first element of array?
Second: You use “foreach” and it means it will print multiple values, if activities has more than 1 element, but I need only one element from array.

What I have tried:

  1. <<[activities[0]]>> - does not work
  2. <<foreach [t in activities]>><<[t.IndexOf()==0? t_Text:""]>><> - does not work

Could you please suggest another option?

Aspose Word version aspose-words-21.5 for Java.

Thank you.

@vova.k,

Please check these two relevant quotes from documentation (Accessing JSON Data):

  1. If a root JSON element is an array, a JsonDataSource instance should be treated as a sequence of items of this array. Although this statement relates to a root JSON element, in fact, all JSON arrays are treated the same: They are loaded as sequences of array items. So array operations become unavailable. However, all sequential operations are available, so a LINQ extension method such as First() can be used instead.

  2. To reference a JSON object property that is an array of simple-type values, the name of the property (for example, “Child”) should be used in a template document, whereas the same name with the “_Text” suffix (for example, “Child_Text”) should be used to reference the value of an item of this array.

Combining these all together, please use the following template syntax for your use case:

<<[activities.first().activities_Text]>>

1 Like

Hello @awais.hafeez

Thank you for your support, it works now!!!
As I am not from C# world, could you please point me to the official MS documentation on sequences?(btw. what class is that?) That would help me with further questions regarding available methods.

Thanks again.

Hello @awais.hafeez

I guess I found the documentation, the class is Enumerable and the docu is

Another questions please -

  1. what version of .NET Linq 21.5 based on?
  2. Does Linq use System.Linq API from .NET? see https://docs.microsoft.com/en-us/dotnet/api/system.linq?view=net-5.0

Thank you!!!

@vova.k,

LINQ Reporting Engine of ‘Aspose.Words for Java’ does not use .NET LINQ internally. The engine mimics .NET LINQ to provide similar capabilities for template syntax. The subset of LINQ extension methods supported by the engine in Java can be found in the following page:

@awais.hafeez got it! thank you a lot for the reference! This topic can be closed.

@vova.k,

In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.