GroupDocs.Assembly for .NET enables you to generate documents directly from JSON data sources. The JsonDataSource class automatically parses JSON and makes it available to your templates, supporting both simple and complex JSON structures.
Here are the steps to generate documents from JSON:
Create or obtain JSON data (from file, API, or string)
Instantiate JsonDataSource with the JSON data
Create a template document with expression tags referencing JSON properties
Use DocumentAssembler to assemble the template with JsonDataSource
Save the assembled document
Note
JsonDataSource automatically recognizes JSON types (numbers, booleans, dates, strings) and makes them available with proper types in templates, enabling numeric operations and date formatting.
JSON written by Json.NET with preserved references holds an object once, marked with $id, and refers to it from other places with $ref. Since version 26.11, JsonDataSource resolves such references: $ref gives a copy of the object it names, so a template reads it like any nested object.
With this data, <<[o.Customer.Name]>> gives A for both orders. An object may also refer to an object holding it, such as an order that refers to the customer holding the order: the simple values of that object are available, and its member that would repeat the path (Order.Customer.Order) gives the parent object. A $ref without a matching $id is ignored. See References between objects for details.
Note
Nested objects of JSON data are compared by value in GroupBy and Distinct: GroupBy(o => o.Customer) puts orders of equal customer objects into one group.
Validate Templates with a JSON Schema
When a member referenced by a template is null, an empty array or missing in the data, the template fails even if the reference is in a branch of <<if>> that is not rendered. Pass a JSON Schema describing the data to JsonDataSource, and the template is checked against the schema instead:
Members declared in the schema can be referenced even if they are absent in the data, members not declared in the schema still fail, and value types come from the schema: a string holding digits, such as "00042", stays a string. See Using a JSON Schema for a complete example and the supported schema keywords.
Configure JSON Data Load Options
You can configure how JsonDataSource parses JSON:
usingGroupDocs.Assembly;usingGroupDocs.Assembly.Data;publicstaticvoidGenerateWithJsonOptions(){JsonDataLoadOptionsoptions=newJsonDataLoadOptions();options.AlwaysGenerateRootObject=true;// Always create root objectoptions.SimpleValueParseMode=JsonSimpleValueParseMode.Strict;// Strict type parsingJsonDataSourcejsonDataSource=newJsonDataSource("data.json",options);DocumentAssemblerassembler=newDocumentAssembler();assembler.AssembleDocument("Template.docx","Output.docx",newDataSourceInfo(jsonDataSource,"data"));}
Warning
By default, if a root JSON element is an object with only one property that is an object or array, the engine skips the root object. Set AlwaysGenerateRootObject to true to always include it.
Advanced Usage Topics
To learn more about working with complex JSON structures, date-time parsing, and advanced JsonDataSource options, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: