You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 17
Next »
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.
Import the JSON Filter Tutorial component using +FROM URL
UseCreate component +FROM URL to import the component.
Copy paste the following URL:
git@bitbucket.org:interfaceware/json_tutorial.git
This component includes Sample Data to make it easier to learn how to filter messages.
Click on the Customize then the Copy and Edit button so you can access the Translator and alter the code
Refer to Customizing components to see how it's done if you have not done this before.
Use the Annotation Window to view your sample data in the String Viewing Window
Now we are in the Translator!
Every Translator component contains a Main.lua module with a main function that is called each time a message is received or on a configured poll time.
Notice we can see our sample data (variable named Data) passed to main function by looking at the Annotation Windows.
If we click on the sample data in the annotations, we can view it using The string viewing window.
Parse the JSON sample data using json.parse{data=Data}
In main, parse the JSON sample data by passing Data.
local patient = json.parse{data=Data}
JSON objects are parsed into Lua tables as dictionaries with key-value pairs. You can see this clearly in the Annotation Window.
Create a filter.lua module to start building a filter function
Use the Create a File button to create a new filters.lua file in your Project Tree. Click Create.
Notice that when you create a new lua file, Iguana will automatically add a require statement to main.lua.
See /wiki/spaces/IXB/pages/3181903910 for why we created a separate lua module specifically for filtering.
Create a filter function in filters.lua
Notice we are brought to an empty filters.lua script in the project tree. Lets add our filtering logic:
First, create your filter function Block statements 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, when we go back into filter.lua, there is now an annotation window we can use to add the rest of the logic.
In our filter, first split the patient contact email to isolate the email domain name
Using String:split(), 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)
Use an if statement to test if an email domain does not equal to "interfaceware.com"
Using if statements, we can test conditions to be false and filter out data we are not interested in processing.
if domain == 'interfaceware.com' then
return true
else
return false
end
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.
Use another if statement in main to test if a message should be queued or filtered out
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 How to push messages into a queue?.
Now let's see our filtering in action by going through the sample data and viewing the annotations
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.
Switch to the second sample data to see the sample message fail the condition and return false.
Add custom logging to your filter to make it more comprehensive
Now that the annotations are active for the failed scenario, we can leverage them to add Custom Logging 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
end
You’re done the filter! Don’t forget to Commit and Push Changes to Git.