Working with JSON
Several built-in processor support dynamic types such as maps/dictionaries and lists/collections.
The secret to processing JSON in Templater is converting JSON input into such dynamic types.
Many Java libraries do that by default, while in .NET
additional conversion
is required to get native dictionaries/values.
Example of JSON usage through Console application can be found among
Github examples.
Main benefit of such project is to easily learn/explore how Templater behaves through template and JSON
tweaking by looking at the output result after running the console application.
An example Invoice JSON
{
"number": 1234,
"date": "today",
"paid": true,
"customer": {
"name": "Satisfied customer Ltd",
"address": "Worldwide"
},
"items": [
{"product": "Templater license", "quantity": 2, "price": 199.00},
{"product": "Documentation", "quantity": 1, "price": 0.00}
]
}
Will be converted into appropriate maps, lists and values (numbers, booleans, strings,...)
Map(
number: Integer(1234),
date: String(today),
paid: Boolean(true),
customer: Map(
name: String(Satisfied customer Ltd),
address: String(Worldwide)
),
items: List(
Map(product: String(Templater license), quantity: Integer(2), price: Decimal(199.00)),
Map(product: String(Documentation), quantity: Integer(1), price: Decimal(0.00))
)
)
Templater will process it by matching relevant tags:
- [[number]]
- [[date]]
- [[paid]]
- [[customer.name]]
- [[customer.address]]
- [[items.product]]
- [[items.quantity]]
- [[items.price]]
Major distinction from other processing solutions is that there is no for loop for processing items. There is only navigation over its properties. This works by Templater matching relevant [[items. tags in the document and duplicating it depending on the number of elements in a list. For template which looks like:
After Templater matches items intermediary document/template will look like:
Templater will now start processing each element in the list, so appropriate values will end up in the expected position:
By combining metadata and plugins more complex scenarios can be supported with JSON,
such as inserting images into the document.
The prebuilt executables for processing JSON
also support image conversion from
base64 input strings.
This way tag like: [[logo]:image] when matched against relevant JSON:
{
"logo": "iVBORw0KGgoAAAANSUhEUgAAAGQAAABDCAMAAABdlVDoAAADAFBMVEUsNzctNzguODguODkvOTowOjowOjsxOzwyPDwzPD00PT00Pj41Pj82P0A3QEE4QUE4QUI5QkM7Q0Q7REQ7REU8REU9RUY+Rkc/R0g/SEhAR0hASEhASUlBSUpCSkpCSktES0xETExFTU1GTk5HTk9IT09IUFBKUVFKUlJMU1NMVFROVVVPVlVQ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