Write a JSON File using FreeMarker Templates
Updated: Nov 10, 2023
This example shows how to write records to a JSON file using FreeMarker templates. It provides a customizable approach to generating JSON output based on templates.
FreeMarker template follows the MVC pattern to write dynamic content to JSON without manually changing the template. By implementing this example you can perform a variety of JSON-based transformations, some of which include:
- dynamically updating data models
- customizing output structures
- seamlessly integrating with other JSON-based systems or APIs.
Input Files
The following files are being used as templates:
header.json
{ "actors": { "north-america": [
detail.json
{ "stageName": "${stageName}", "realName": "${realName}", "gender": "${gender}", "city": "${city}" <#if (balance >= 0)> ,"balance": "${balance}" </#if> },
footer.json
] } }
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook; import com.northconcepts.datapipeline.core.*; import com.northconcepts.datapipeline.job.Job; import com.northconcepts.datapipeline.memory.MemoryReader; import com.northconcepts.datapipeline.template.TemplateWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteAJsonFileUsingFreeMarkerTemplates { public static void main(String[] args) throws IOException { Record record1 = new Record(); record1.getField("stageName", true).setValue("John Wayne"); record1.getField("realName", true).setValue("Marion Robert Morrison"); record1.getField("gender", true).setValue("male"); record1.getField("city", true).setValue("Winterset"); record1.getField("balance", true).setValue(156.35); Record record2 = new Record(); record2.getField("stageName", true).setValue("Spiderman"); record2.getField("realName", true).setValue("Peter Parker"); record2.getField("gender", true).setValue("male"); record2.getField("city", true).setValue("New York"); record2.getField("balance", true).setValue(-0.96); RecordList recordList = new RecordList(record1, record2); DataReader reader = new MemoryReader(recordList); TemplateWriter writer = new TemplateWriter(new FileWriter("example/data/output/credit-balance-04.json")); writer.setFieldNamesInFirstRow(false); writer.getConfiguration().setDirectoryForTemplateLoading(new File("example/data/input")); writer.setHeaderTemplate("WriteAJsonFileUsingFreeMarkerTemplates-header.json"); writer.setFooterTemplate("WriteAJsonFileUsingFreeMarkerTemplates-footer.json"); writer.setDetailTemplate("WriteAJsonFileUsingFreeMarkerTemplates-detail.json"); Job.run(reader, writer); } }
Code Walkthrough
- Beginning the execution, two Record instances are created and aggregated into a RecordList.
- A
MemoryReader
is used to initialize thereader
object with the prepared record list. - Then, a
TemplateWriter
is configured with the output file path, template directory, and template filesheader.json
,footer.json
,detail.json
sections. - Finally,
Job.run()
method is invoked withreader
andwriter
objects to store the output tocredit-balance-04.json
file.
Output File
The following JSON is populated in the credit-balance-04.json
file:
{ "actors": { "north-america": [{ "stageName": "John Wayne", "realName": "Marion Robert Morrison", "gender": "male", "city": "Winterset" ,"balance": "156.35" },{ "stageName": "Spiderman", "realName": "Peter Parker", "gender": "male", "city": "New York" },] } }