JSON is a lightweight, plain text data format. It’s become a standard that is widely used across many industries, even including healthcare.
Its simplicity simple structure makes it a good example to review some key Lua and Translator concepts. Lets start!
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.
Expand | ||||
---|---|---|---|---|
|
| |||
UseCreate component +FROM URL to import the component. Copy paste the following URL:
This component includes Sample Data to make it easier to learn how to filter messages. |
Expand | ||
---|---|---|
| ||
Refer to Edit a Component to see how it's done if you have not done this before. |
...
Expand | ||||
---|---|---|---|---|
| Now ||||
Now we are in the Developing in the Translator! Every Translator component contains a Main.lua module with a main Functions that is called each time a message is received or on a configured poll time. Notice we can see our sample data (Variables named Data) passed to main functionby looking at the Annotation Windows. If we click on the sample data in the annotations, we can view it using The string viewing window in Escaped Text view. Notice, how the JSON object is formatted. Each line ends with a newline “\n” character. We can use this to parse and process the JSON. |
Expand | ||
---|---|---|
| ||
You can use String:split() to split the function on (“\n”):
By Using trace() function, we can click on the List in the annoations. You should see the following: This string is now split into a table with individual lines you can now process. This is great... but with Iguana, we’ve made it a little easier to efficiently parse and process JSON. |
Expand | |||||
---|---|---|---|---|---|
| |||||
JSON objects are parsed into Lua tables. You can see this clearly in the Annotation Window. |
...
...
. |
Expand | |||||
---|---|---|---|---|---|
| |||||
In main, parse the JSON sample data by passing Data. Create and assign the parsed data to a patient variable.
JSON objects are parsed into Lua tables as dictionaries with key-value pairs. You can see this by clicking the resulting table in the Annotation Window. |
Expand | ||
---|---|---|
| ||
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. |
Expand | |||||
---|---|---|---|---|---|
| |||||
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 statements and pass patient as the parameter.
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 Windows, notice there is now an annotation block we can use to add the rest of the logic. |
Expand | |||||
---|---|---|---|---|---|
| |||||
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:
Specifying the second result [2] of the split table, I can capture just the domain name. |
Expand | ||
---|---|---|
| ||
Using if statements, 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. |
Expand | ||
---|---|---|
| ||
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. |
Expand | ||
---|---|---|
| ||
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. |
Expand | |||||
---|---|---|---|---|---|
| |||||
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,
You’re done the filter! Don’t forget to Commit and Push Changes to Git. |
Now back to the Orientation!