Building an automation using JSON actions - Build a Task Bot that uses JSON actions to convert a dictionary to JSON, insert and update nodes, and convert the JSON back to a dictionary.

Automation 360

Build a Task Bot that uses JSON actions to convert a dictionary to JSON, insert and update nodes, and convert the JSON back to a dictionary.

Before you begin

Ensure that you have the required roles and permissions to create and run Task Bots.

Procedure

  1. Log in to Control Room.
  2. Perform the following steps to create a Task Bot:
    1. On the left panel, click Automation.
      The Private repository of the Control Room displays.
    2. Select Create > Task Bot.
    3. Enter Json_Test_Bot in the Name field.
    4. Click Create and Edit.
      The Task Bot editor page displays.
  3. Perform the following steps to create the required variables:
    1. Go to Variables on the left menu and select Create variable.
    2. Specify the following in the Create variable dialog that appears.
      • Type: Dictionary
      • Subtype: Any
      • Name: vSourceDictionary
      • Default values:
        Type Key Value
        String firstName Mathew
        String LastName Brain
        Number id 5669
        Boolean isAdmin True
    3. Select Create to create the dictionary variable.
    4. Create two more dictionary variables named vAddressDictionary and vDestinationDictionary using the same procedure.
      Ensure that the Default values section is empty for both dictionary variables.
    5. Go to Variables on the left menu and select Create variable.
    6. Specify the following in the Create variable dialog that appears.
      • Type: List
      • Subtype: String
      • Name: vHobby
      • Default values:
        1. Value at 0: Reading
        2. Value at 1: Football
        3. Value at 2: Cooking
    7. Select Create to create the list variable.
    8. Go to Variables on the left menu and select Create variable.
    9. Specify the following in the Create variable dialog that appears.
      • Type: String
      • Name: vJson
    10. Select Create to create the string variable.
  4. Double-click or drag the Dictionary > Put action to assign a value to a key in the dictionary.
    1. Select the vSourceDictionary variable from the drop-down list.
    2. Enter tags in the Key field.
    3. Select the Variable tab, and then select vHobby as the value from the drop-down list.
  5. Double-click or drag the JSON utilities > Convert Dictionary to JSON action to convert the dictionary variable to JSON and store it in a string variable.
    1. Select the vSourceDictionary variable from the drop-down list.
    2. Select vJson in the Save the outcome to a variable field.
  6. Double-click or drag the Message box action to display the values.
    1. Enter $vJson$ in the Enter the message to display field.
  7. Double-click or drag the JSON utilities > Start sessionaction to start a JSON session.
    1. Select the Payload option and enter $vJson$.
    2. Retain Default as the session name in the Local session option.
  8. Double-click or drag the JSON utilities > Insert node action to add a new value to the existing JSON.
    1. Retain Default as the session name in the Session name field.
    2. Enter $$ in the Node path field.
    3. Enter address in the Key name field.
    4. Select Dictionary (object) in the Value type field.
    5. Select the Dictionary tab in the Value field and enter the following values:
      Type Key Value
      String city Houston
      String state Texas
      String country US
      Number pincode 100233
      Boolean isPermanentAddress True
  9. Double-click or drag JSON utilities > Get node value action to retrieve the node values.
    1. Retain Default as the session name in the Session name field.
    2. Enter $$ in the Node key or path field.
    3. Select SampleString in the Assign output to a variable field.
      The SampleString variable is available as one of the predefined variables.
  10. Double-click or drag the Message box action to display the values.
    1. Enter $SampleString$ in the Enter the message to display field.
  11. Double-click or drag JSON utilities > Update node value action to update a value in the existing JSON.
    1. Retain Default as the session name in the Session name field.
    2. Enter tags in the Node path field.
    3. Select String in the Value type drop-down.
    4. Enter Running in the Value (optional) field.
  12. Double-click or drag JSON utilities > Update node value action to update a value in the existing JSON.
    1. Retain Default as the session name in the Session name field.
    2. Enter LastName in the Node path field.
    3. Select String in the Value type drop-down.
    4. Enter Wade in the Value (optional) field.
  13. Double-click or drag JSON utilities > Get node value action to retrieve the node values.
    1. Retain Default as the session name in the Session name field.
    2. Enter $$ in the Node key or path field.
    3. Select SampleString in the Assign output to a variable field.
  14. Double-click or drag the Message box action to display the values.
    1. Enter $SampleString$ in the Enter the message to display field.
  15. Double-click or drag JSON utilities > Convert JSON to Dictionary action to convert the JSON string to a dictionary.
    1. Retain Default as the session name in the Session name field.
    2. Select the Dictionary option, and then select vDestinationDictionary from the drop-down list in the Assign output to a variable field.
  16. Double-click or drag the Dictionary > Get action to verify whether a key exists in a dictionary variable and return the corresponding value.
    1. Select vDestinationDictionary from the Dictionary variable drop-down.
    2. Enter address in the Key field.
    3. Select the vAddressDictionary variable from the Assign the output to variable drop-down.
  17. Double-click or drag the Message box action to display the values.
    1. Enter the following message in the Enter the message to display field:
      Output Dictionary:
      Size :$vDestinationDictionary.Dictionary:size.Number:toString$
      Key[id]: $vDestinationDictionary{id}$
      Key[address.city]: $vAddressDictionary{city}$
  18. Double-click or drag JSON utilities > End session action to close the JSON session.
    1. Retain Default as the session name in the Session name field.
  19. Click Save to save the bot.

Results

When you run the bot, the configured message boxes display the following messages:
  1. The Convert Dictionary to JSON action converts the data in the vSourceDictionary variable to a JSON string and saves it in the vJson variable. The message box displays the string:
    Message box displaying the data from the dictionary variable that is converted to a JSON string
  2. The Insert node action inserts the address key in the JSON string. The Get node value action saves the updated JSON string in SampleString. The message box displays its contents:
    Message box displaying the JSON string amended using the Insert node and Get node value actions
  3. The Update node value action adds Running to the tags list and updates the LastName value to Wade. The Get node value action saves the updated JSON string in SampleString, and the message box displays its contents:
    Message box displaying the JSON string updated using the Update node value action
  4. The Convert JSON to Dictionary action converts the updated JSON string to a dictionary variable and saves the data in vDestinationDictionary. The message box displays its contents:
    Message box displaying the dictionary size and the id key from vDestinationDictionary and the city key from vAddressDictionary