Skip to main content

Using variables

Did the previous step — making the sales percentages and their respective offices appear in descending order — seem particularly tedious and heavy on the ATL? That’s because there’s a better way of using the sort function! Let’s create a variable with it, to smooth things out a bit. Let’s call our new variable SuccessfulOffices.

  1. In the TotalSales script, select the entire ATL snippet — the first one — that begins [[map(sort(filter… and copy it. You are selecting and copying the following:

    [[map(sort(filter(WholeJSON.offices, office -> office.sales > office.target), (a,b) -> sign(percentage(b.sales-b.target, b.target) - percentage(a.sales-a.target, a.target))), office -> office.name)]]

  2. Click Variables to go to the Variables view.

  3. In the Variables view, click + NEW VARIABLE.

  4. In the VARIABLE NAME field, enter SuccessfulOffices.

  5. In the VARIABLE TYPE field, select LIST from the drop-down.

  6. In the SOURCE field, paste the ATL snippet you copied in Step 1.

  7. Now, before we finish defining our new variable, we just need to tidy up the contents of the SOURCE field a bit.

    1. At the beginning of the snippet, remove map(.

    2. At the end of the snippet, remove , office -> office.name).

    We took these bits of ATL out because the variable will focus on sorting only, not on looping through offices. Your SOURCE field contents should now look like this:

    [[sort(filter(WholeJSON.offices, office -> office.sales > office.target), (a,b) -> sign(percentage(b.sales-b.target, b.target) - percentage(a.sales-a.target, a.target)))]]

    Using Variables JSON 01.png
  8. Click Done.

    Now we'll call this new variable, using it to replace the sort and filter functions.

  9. Return to the Compose view, then in the TotalSales script replace this ATL: sort(filter(WholeJSON.offices, office -> office.sales > office.target), (a,b) -> sign(percentage(b.sales-b.target, b.target) - percentage(a.sales-a.target, a.target)))

    with SuccessfulOffices, like this:

    The best sales were in [[map(SuccessfulOffices, office -> office.name)]]

    Note

    Depending on the cutting and tidying you did to this ATL, you may need to remove a closing parenthesis following SuccessfulOffices. Check that you have no closing parenthesis following SuccessfulOffices.

    Now we need to use the SuccessfulOffices variable again, this time in the second part of that sentence that reports on offices that exceeded their sales targets.

  10. In the map snippet after which exceeded their sales target by, replace this ATL:

    sort(filter(WholeJSON.offices, office -> office.sales > office.target), (a,b) -> sign(percentage(b.sales-b.target, b.target) - percentage(a.sales-a.target, a.target)))

    with SuccessfulOffices, like this:

    which exceeded their sales target by [[map(SuccessfulOffices, office -> joinStrings(percentage(office.sales-office.target, office.target),"%"))]] respectively.

  11. Go to the Main script and preview your narrative. Your finished narrative should look like this:

    Using Variables JSON 02.png

    Let’s create another variable to further simplify the code. This time the variable is of type JSON. JSON variables refer to specific sections within the JSON data. We are going to create the new JSON variable Offices to refer to WholeJSON.offices.

  12. Click Variables to go to the Variables view.

  13. Click + NEW VARIABLE.

  14. In the VARIABLE NAME field, enter Offices.

  15. In the VARIABLE TYPE field, select JSON from the drop-down.

  16. In the SOURCE field, click on Choose root and choose WholeJSON, then click on Next node and choose offices from the drop-down:

    Using Variables JSON 03.png
  17. Click Done.

  18. Now click on SuccessfulOffices (in the list of variables) to edit that variable.

  19. In the SOURCE field, replace WholeJSON.offices with our new Offices variable. You should now have:

    [[sort(filter(Offices, office -> office.sales > office.target), (a,b) -> sign(percentage(b.sales-b.target, b.target) - percentage(a.sales-a.target, a.target)))]]

  20. Click Done. The list of variables should now look like this:

    Using Variables JSON 04.png
  21. Preview your narrative to check that it generates the same text.

    Using Variables JSON 02.png

    You’ve successfully created a JSON variable and referenced it in another variable.

In this step of the tutorial, we learned that creating user-defined variables can make things so much simpler!

Next up, we’re ready to publish this project.

Publishing your JSON project