Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

JSON is a lightweight, plain text data format. It’s become a standard that is widely used across many industries, even 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.

Lets start!

See

Use the following file:

Expand
titleAdd Sample Data by importing the provided sample file
View file
namepatient.txt

Import the JSON Filter Tutorial component using +FROM URL

This component includes sample JSON data to make it easier to learn how to filter messages.

You can useCreate component +FROM URL to import the component.

Copy paste the following URL:

Code Block
git@bitbucket.org:interfaceware/json_tutorial.git

If you don’t know how, see Sample Data for how to add sample files to a project.

Expand
titleClick 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.

Expand
titleUse the Annotation Window to view your sample data in the String Viewing Window as Escaped Text.

Now we can see our sample data passed to main 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 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.

Image Removed

Expand
titleUse string.split() to parse the JSON sample data

You can use String:split() to split the function on (“\n”):

Code Block
languagelua
local List = Data:split("\n")
trace(List)

By Using trace() function, we can click on the List in the annotations and see the following:

This string is split into a Lua table as list you can now begin to process. This is great... but with Iguana, we’ve made it a little easier to efficiently parse and process JSON.

Image Removed

.

Image Added
Expand
titleParse the JSON sample data using json.parse{data=Data}
Code Block
languagelua
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.

Using if statements, we can conditionally map new values when the specified condition is true
Expand
titleUse an if statement to map a new value for the state "CA" to "California"
First, isolate the email providers

Using String:split(), we can split the email on “@“ and get the domain name.

Code Block
languagelua
local stateemail = patient.addresscontact.stateemail
iflocal statedomain == email:split("CA" then 
   state = "California"    
end 
trace(state@")[2]
trace(domain)

...

Expand
titleImport the Codemap Library

if statements

...

Use an if statement to ignore email domains not equal to "interfaceware.com"

Using if statements, we can filter out data we are not interested in processing. For example, say we only want to process data including the interfaceware domain.

Code Block
languagelua

...

if 

...

domain =

...

= 'interfaceware.com' then
  queue.push{data=

...

Data}
else 
  return 
end 

See How to push messages into a queue?

Expand
titleNow lets see our filtering in action by flipping through the sample data and viewing the annotations.