Creating a JSON Filter
JSON is a lightweight, plain text data format. It’s become a standard that is widely used across many industries, including healthcare.
Its simple structure makes it a good example to review some key Lua and Translator concepts.
Let's set up a simple filter component that parses a JSON object and ignores data with emails not containing the "interfaceware.com" domain name.
Usehttps://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684453620 to import the component.
Copy paste the following URL:
git@bitbucket.org:interfaceware/json_tutorial.gitThis component includes https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2689269799 to make it easier to learn how to filter messages.
Refer to https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2679931435 to see how it's done if you have not done this before.
Now we are in the https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684355322!
Every Translator component contains a https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2689925128 module with a main https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2685304936 that is called each time a message is received or on a configured poll time.
Notice we can see our sample data (https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2690777203 named Data) passed to main function by looking at the Annotation WindowsPreview.
If we click on the sample data in the annotations, we can view it using https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684453829.
In main, parse the JSON sample data by passing Data. Create and assign the parsed data to a patient variable.
local patient = json.parse{data=Data}JSON objects are parsed into https://interfaceware.atlassian.net/wiki/spaces/IXB/pages/2684322676 with key-value pairs. You can see this by clicking the resulting table in the Annotation Window.
Use the Create a File button to create a new filters.lua file in your Project TreePreview. Click Create.
Notice that when you create a new lua file, Iguana will automatically add a Require StatementPreview to main.lua.
Notice we are brought to an empty filters.lua script in the project tree. Lets add our filtering logic:
First, create a filter function Block statementsPreview and pass patient as the parameter.
function filter(patient)
end Then go into main and call your filter function, passing your parsed patient JSON as the expected parameter.
Notice after calling our filter function,
Navigate back to the filter.lua by clicking the purple filter in the Annotation WindowsPreview, notice there is now an annotation block we can use to add the rest of the logic.
Using String:split()Preview, we can split the email on “@“ and get the domain name.
Try this code and take a look at your annotations to see the result:
local email = patient.contact.email
local domain = email:split("@")[2]
trace(domain)Specifying the second result [2] of the split table, I can capture just the domain name.
Using if statementsPreview, we can test conditions to be false and filter out data we are not interested in processing:
Now lets go back to main and change the way we are calling our filter function to address the boolean values (true/false) being returned.
Your main function should look like this:
This if statement is testing if the filter function returns true - the domain name equalled “interfaceware.com” - then queue the message to be processed by the next component.
See Push Messages to a Component QueuePreview.
By switching which Sample Data is being passed to main, we can see the results and test how our filter function works, by using the annotations.
Navigate to filters.lua and switch to the second sample data to see the sample message fail the condition and return false.
Now that the annotations are active for the failed scenario, we can leverage them to add Custom LoggingPreview to add more visibility into the logs.
For example,
if domain == 'interfaceware.com' then
return true
else
iguana.log('Filtered out message with: '..domain)
return false
endYou’re done the filter! Don’t forget to Commit and Push Changes to GitPreview.
Now back to the Getting StartedPreview!