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.
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: