When using the ReportingEngine with a JsonDataSource, we have a mixed item array being passed in.
The array looks like
{
"items": [
{ "q": "Question 1", "a": "Simple Answer"},
{ "q": "Question 2", "a": { "amount": "5000", "currency": "GBP" }}
]
}
When we merge it, we use an if to check if the currency field is not null, if it isn’t, we output the currency and amount into the document. That bit works fine.
But when we output just the a property, we get what looks the internal object from the ReportingEngine instead, as the document contains the text RJ.
Can you advise on a way forward?
@simon.cooper It is not quite clear what the problem is. Could you please elaborate it in more details and provide you template, code, current output and expected output documents. This will help us to better understand your requirements.
Sure,
This is sample code to generate it, tested this under .net6.0 with Aspose.Words 23.9.
using Aspose.Words.Reporting;
using Aspose.Words;
using System.Text;
string jsonFileName = Path.GetTempPath() + "Json_Source.docx";
ReportingEngine reportingEngine = new() { Options = ReportBuildOptions.AllowMissingMembers | ReportBuildOptions.RemoveEmptyParagraphs };
reportingEngine.KnownTypes.Add(typeof(ControlChar));
Document document = new();
document.EnsureMinimum();
document.MailMerge.UseNonMergeFields = true;
DocumentBuilder builder = new(document);
builder.MoveToDocumentStart();
builder.InsertParagraph();
builder.Write("<<foreach [i in items]>>");
builder.Write("<<[i.q]>><<[ControlChar.Tab]>>");
builder.Write("<<if [i.a.currency != null]>>");
builder.Write("<<[i.a.currency]>><<[i.a.amount]>>");
builder.Write("<<else>>");
builder.Write("<<[i.a]>>");
builder.Write("<</if>>");
builder.Write("<<[ControlChar.LineBreak]>>");
builder.Write("<</foreach>>");
builder.Document.Save(jsonFileName, SaveFormat.Docx);
string json = @"
{
""items"": [
{ ""q"": ""Question 1"", ""a"": ""Simple Answer""},
{ ""q"": ""Question 2"", ""a"": { ""amount"": ""5000"", ""currency"": ""GBP"" }},
{ ""q"": ""Question 3"", ""a"": ""Another Simple Answer""}
]
}";
Console.WriteLine(json);
await using MemoryStream jsonStream = new(Encoding.UTF8.GetBytes(json));
JsonDataSource jsonDataSource = new(jsonStream, new JsonDataLoadOptions() { AlwaysGenerateRootObject = true });
reportingEngine.BuildReport(document, jsonDataSource, "DataSource");
jsonFileName = Path.GetTempPath() + "Json_Result.docx";
document.Save(jsonFileName, SaveFormat.Docx);
Console.WriteLine(jsonFileName);
When you run that, you will get output like this in the document.
Question 1 nK
Question 2 GBP5000
Question 3 nK
I hope that helps illustrate the issue
@simon.cooper
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-25955
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@simon.cooper
Unfortunately, JsonDataSource
internal implementation does not allow to work with JSON like this. However, you can slightly change JSON data in the following way to make it work:
{
"items": [
{ "q": "Question 1", "sa": "Simple Answer"},
{ "q": "Question 2", "a": { "amount": "5000", "currency": "GBP" }}
]
}
Please note that the template should be adjusted accordingly as follows then:
<<foreach [i in items]>><<[i.q]>><<[ControlChar.Tab]>><<if [i.a?.currency != null]>><<[i.a.currency]>><<[i.a.amount]>><<else>><<[i.sa]>><</if>><<[ControlChar.LineBreak]>><</foreach>>
Thanks, I thought that might be the answer.
I’ll workaround the shortfall in functionality
1 Like