Dacris Maestro Framework comes with a powerful templating system for text. The design goal behind this templating system is to enable app developers to transform any structured data (JSON / SQL) into unstructured text documents quickly and easily, with maximum flexibility. In this tutorial, we will try to build such a translating application. We will create a Maestro logic app that translates from a provided JSON file (input) to a SQL script consisting of multiple INSERT statements (output).
Let's start by supposing you have a CMS, and that CMS spits out a JSON array of your content. This is the case with OLISP (Dacris Software's own CMS).
Here is an example of JSON:
{ "content": [{"Body": "hello world", "Tags": "home hello"}, {"Body": "how are you?", "Tags": "home question"}] }
The above JSON defines an array of content items. The first item has the body "hello world", while the second item says "how are you?" The items are also each associated with a set of tags (keywords) for search.
Save the above JSON example as OlispData.json, so we can import it later using our app.
In Maestro Framework, templates can be nested with multiple levels of depth. In this tutorial, we will create a two-level template. The first template we will create is an overall (master) template for the whole set of content. This template will run only once, for the whole data set.
Template 1:
#@$.content@
Note - Please include a blank new line at the end of each template. This tells Maestro Framework to do a line break.
Save template 1 as SqlTemplate.txt.
Template 2:
INSERT INTO content (Body, Tags) VALUES ('#@$.level2Item.Body@','#@$.level2Item.Tags@');
Save template 2 as SqlInnerTemplate.txt.
Let's take a step back and examine these templates. The #@...@ is the syntax for referencing an item within the Maestro app state. The path inside the #@...@ is a JSONPath expression, e.g. $.content, which means lookup the root of the JSON and find the entry "content". Inside the second template, we have level2Item. This refers to the current item as the content gets iterated. The second template is repeatedly called, once for each object inside the "content" JSON array. The current object is called level2Item. For a 3rd level template, this would be called level3Item, and so on.
The app logic is pretty simple. It consists of just 5 lines of code:
alias Dacris.Maestro as the read state from file using the core merge state from file using the core with olispDataFile txt file from template using the core with templateFile
That's it. Save this code inside OlispImport.txt. It's in plain English format, which allows readers to understand the logic quite easily. Output will be saved as Output.txt.
The final step is to configure the initial application state in State.json:
{ "olispDataFile": "OlispData.json", "templateFile": "SqlTemplate.txt", "level2TemplateFile": "SqlInnerTemplate.txt" }
Save the above JSON inside a file called State.json. Notice how the second-level template is in a file referenced by level2TemplateFile. This is always going to be the case - this config key name is predefined.
Use the LogicAppRunner at the command line to execute the app using a command like this:
(Be sure you're in your application's working directory)
dotnet <path-to-LogicAppRunner.dll> OlispImport
The output will be saved to Output.txt.
That's it! You now have a fully working JSON-to-SQL importer built with Maestro Framework in just 10 lines of code. Hopefully you understand now how to use Maestro string templates, and how the nested template concept can be so powerful. If you wanted to get fancy, you could connect your Maestro Framework app directly to a SQL database with just a few more lines of configuration and code. The result would be that instead of calling string templates to output INSERT statements, your app would push the content directly into a SQL database. You can refer to the SqlServer app inside the TestApps folder of the Maestro Framework package to get an example of how that can be done.