I just love the fact that Kapow allows you to automate almost anything. Anything, you say? Well, how about a Robot that builds parts of itself? This post will show you how to create a type directly from a Robot, ready for use for another Robot. Sounds kinky?
I’m working with web services on a daily basis. And while working with RESTful services and even SOAP is incredibly easy in Kapow, there’s one thing that always bothered me: most web services nowadays return JSONĀ (that’s actually not the part that bothers me, I like JSON). But when I wanted to do something with that result, I first had to create a type in Kapow to hold relevant data I wanted to work with (note that there is a mapper available for XML, but not for JSON). So: let’s create a Robot that can build a type. Here’s my sample JSON:
{
"name" : "MasterCheckDigitCheck",
"text" : "OK",
"valid" : true,
"errorDescription" : "",
"left" : 28,
"top" : 356,
"height" : 13,
"width" : 521,
"pageIndex" : 0,
"confidence" : 1,
"formattingFailed" : false,
"fieldAlternatives" : []
}
This is a sample from Kofax’ very own Real-Time Transformation Interface (RTTI) which allows gives you a RESTful service hosting any Transformation Project – I selected this sample because of its different types (there are strings, booleans, and integers). Before we get started, let’s have a look at Kapow’s type definitions. Depicted below is the type for my RTTI response on the left hand side – and if you ever tried to open such a .type file, you’ll see that it’s essentially an XML as shown on the right hand side:
And this is exactly what we will do: we will create a Robot that will then create a type from scratch by writing an XML file. Note that there are other ways to do so, you could even use Desktop Automation to create a new type with Design Studio, but that’s a different story. Before going into all the details, here’s an overview of the working Robot:
The basic concept is simple:
- Loop over each property from the JSON and extract its name (e.g. “pageIndex”),
- Extract the value of said property and determine its type (e.g. 0 -> integer, 1.23 -> number, true -> boolean, everything else -> string),
- Append to the type XML and write it at the end.
You will also note that there are two templates being mentioned: a Type Template and an Object Template. The Type Template is something like the skeleton of the final XML file, while the Object Template will be applied for each property present in the JSON. Here’s an example with two attributes being present, “name” and “text”.
Note that both the type and object templates are held in a JSON variable with placeholders. The most important steps in that regard are shown below. Here’s what’s going on (click to enlarge the image):
- The action Extract Property Integer Value parses the property for a numeric value. In case it fails the second branch will be tried, and so on (imagine a string value – it would fail all three branches and default to the last one).
- The action Open Object Template just shows the JSON object variable – our template, and the Set Name, Set Type and Set Default actions are responsible for replacing placeholders with their respective values.
- The Open Template and Insert Content actions append the newly created object template into the type template.
At the end of the For Each loop, the XML file is simply written to disk – your robot has just created a type for you! You could even create a Kapplet for this Robot, paste a sample JSON there, and find the newly created type automatically in Design Studio – even without reloading anything:
Conclusion and Outlook
The only drawback here is that Kapow does not support nested types. While we still could easily deal with arrays, nested arrays or even JSON objects cannot be represented in a type. Of course you could just create additional types, or let the JSON hold your data for as long as possible.
However, the automation madness does not end here. While I just showed how a Robot can create parts for another Robot, but you could even let Robots build other Robots – as the whole configuration is nothing but an XML file. Let me know what you built.