Skip to main content

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.

  1. 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:

    1. has only been with us for a few months

    2. has been part of the company for years

  2. For the manager’s name, insert [[office.manager.firstName]] in place of A.

  3. Place your cursor after [[office.manager.firstName]], add a space, and then click Insert Conditional to open the Conditional Builder dialog.

    ACV JSON 01 (new).png

    In our JSON data, in the manager object, we have yearsEmployed. We’re going to use yearsEmployed in our report to mention each manager’s tenure with the company.

  4. In the Conditional Builder, make Case 1 be for tenures less than one year:

    1. In the PARAMETER A field, enter [[office.manager.yearsEmployed]].

    2. Change the CONDITION field for Parameter A to Is Equal To.

    3. In the PARAMETER B field, type 0.

    4. In the RESULT field, type: has only been with us for a few months.

  5. 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.

  6. 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.

    ACV JSON 02 (new).png

    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.)

  7. Click Build.

  8. 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}]].

    ACV JSON 03 (new).png
  9. Go to the Main script and click Preview.

    ACV JSON 04 (new).png

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.

Using lambda expressions in higher-order functions