ReportingEngine with JsonDataSource case sensitivity

Hi everyone,

When I use the JsonDataSource with the Reporting engine, first level properties are not case sensitive but nested array or Object property name are case sensitive.

Is it normal? Is there a way to ignore case?
I want to avoid forcing users to respect a certain case for property in word document.

For example in this code:

string startJson = "{\"Test\":{\"Var1\":\"22.50\",\"var2\":{\"foo\":\"2024-01-01\"},\"Objects\":[{\"Foo\":\"this is props foo of first object\"},{\"foo\":\"this is props foo of last object\"}]}}";
Document doc = new Document("input/test.docx");
ReportingEngine report_engine = new ReportingEngine();
report_engine.Options = ReportBuildOptions.AllowMissingMembers;

JObject jsonParsed = JObject.Parse(startJson);
var json = new MemoryStream(Encoding.UTF8.GetBytes(jsonParsed.ToString()));

JsonDataLoadOptions jsonDataLoadOptions = new JsonDataLoadOptions();
jsonDataLoadOptions.SimpleValueParseMode = JsonSimpleValueParseMode.Strict;
jsonDataLoadOptions.ExactDateTimeParseFormats = new List<string>();

report_engine.BuildReport(doc, new JsonDataSource(json, jsonDataLoadOptions), "test");
doc.Save("output/output.pdf");

If we dont follow the case from the Word document in the Json object for nested array, the outputed propery is blank (or will trigger an error depending of AllowMissingMembers).

Thanks for your help,

@Dennis_Knight

Could you please also share the template document referenced in code?

test.docx (12,4 Ko)

@Dennis_Knight

Thank you for providing the details.

The issue appears because JsonDataSource allows case-insensitive access to object properties (if an attempt of case-sensitive access fails), but JSON notation itself is case-sensitive. Since there is an array of objects, for one item of which there is property foo and for another item of which there is property Foo, each array item contains two properties foo and Foo internally (because of case sensitivity of JSON itself). Since JsonDataSource tries case-sensitive access in the first place, it succeeds, hence there are different values printed for foo and Foo in the output document. To recap, this is an expected behavior rather than a bug.

If the same property name (i.e. foo or Foo) is used for all array items in JSON, then every array item contains just a single property internally and any property name such as foo, Foo, or FOO can be used in a template document to reference the property. So, to provide case insensitivity for a template document, JSON data can be changed in the mentioned way.

Ok, I understand.

Thanks a lot for your help :+1:

1 Like