Adding conditional variation to your JSON project
Earlier, we used the chooseAtRandom
function to add random variation to our report. Now let’s add another kind of variation: conditional variation. We’ll use this to describe how long each office’s manager has been working for the company.
In the DescribeOffice script, add the following sentence: A joined the company last year.
A is a placeholder for the office manager. The rest of the sentence forms the phrase we’re going to add conditional variation to. We’re going to provide two additional options that describe the manager’s tenure, as follows:
has only been with us for a few months
has been part of the company for years
For the manager’s name, insert
[[office.manager.firstName]]
in place of A.Place your cursor after
[[office.manager.firstName]]
, add a space, and then click Insert Conditional to open the Conditional Builder dialog.In our JSON data, in the
manager
object, we haveyearsEmployed
. We’re going to useyearsEmployed
in our report to mention each manager’s tenure with the company.In the Conditional Builder, make Case 1 be for tenures less than one year:
In the PARAMETER A field, enter
[[office.manager.yearsEmployed]]
.Change the CONDITION field for Parameter A to Is Equal To.
In the PARAMETER B field, type
0
.In the RESULT field, type: has only been with us for a few months.
Now make Case 2 be for tenures of one year. Repeat Step 4, except use
1
in the PARAMETER B field, and type joined the company last year in the RESULT field.Our default result will be for tenures of more than one year. In the DEFAULT RESULT field, type has been part of the company for years.
Tip
Be careful to use the DEFAULT RESULT field, not the RESULT field for Case 3. (All fields for Case 3 can be left blank.)
Click Build.
Now that you have put the necessary ATL in the script, remove the remaining placeholder text (joined the company last year).
Your new ATL snippet should look like this:
[[office.manager.firstName]] [[if(office.manager.yearsEmployed==0){has only been with us for a few months}elseif(office.manager.yearsEmployed==1){joined the company last year}else{has been part of the company for years}]].
Go to the Main script and click Preview.
In this step of the tutorial, we learned how to add variation to our narrative by building a conditional. Our conditional has two cases for which we specified parameters, plus a third case acting as the default. For this third case, because it is the default, we didn’t need to specify parameters; we only needed to specify the default output.
In the next step, we’ll use anonymous functions with a few higher-order functions.