Using Python to parse JSON response

Use the Automation 360 Python script to execute Python functions to build a bot. Use the Python functions to parse the JSON response from a REST Web Services GET request.

Prerequisites

Ensure you have the following to build the bot:

  • Basic understanding of Python programming language.
  • Basic experience with creating Automation Anywhere bots.
  • Download and install Python 3.

    download Python

    Do one of the following:

    • While installing the Python, make sure to select Add Python x.xx to PATH during the Python installation.
      Install Python
    • Add a path to the Environment variable.

Procedure

  1. Log in to the Control Room.
  2. On the left pane, click Automation.
    A list of available bots and forms is displayed.
  3. Click Create a bot icon.
    Create a bot
  4. In the Name field, enter PythonTutorial.
  5. Click Create & Edit.
  6. In the Actions pane, click Variables to create the following new variables:
    1. Name dResponse, Type > Dictionary and Subtype > String.
    2. Name dRetrieveValue, Type > Dictionary and Subtype > Any.
    3. Name dResponseBody, Type > String.
    4. Name sFullName, Type > String.
    5. Name sLocation, Type > String.
    6. Name sTotalUserCount, Type > String.
    7. Name nTotalUserCount, Type > Number.
    8. Name nCurrentUser, Type > Number.
    9. Name sCurrentUser, Type > String.
    10. Name prompt-assignment, Type > String.
  7. From the Actions pane, select REST Web Services > Get method and place it under the Start of the bot flow.
    1. In the URI field, enter https://randomuser.me/api/?results=5&inc=name,email,location&nat=us.
      This is a sample API that returns random user details to the calling application.
    2. In the Authentication Mode drop-down list, select No Authentication.

      python-rest-sample
    3. In the Assign the output to a variable drop-down list, select dResponse -Dictionary of Strings.
  8. From the Actions pane, click String > Assign action and drag it into the canvas below the REST Web Services action.
    1. In the Select the source string variable value field, enter $dResponse{Body}$.

      python-string-assign
    2. In the Select the destination string variable drop-down list, select dResponseBody - String.
  9. From the Actions pane, click Python script > Openand drag it into the canvas below the String > Assignaction.
    1. In the Python field, select Manual input.

      python-script-open
    2. In the Enter script here field, copy and paste the following code.
      import json
      
      def get_node_count(response):
          #parse response as json
          response_dict=json.loads(response)
          # Create list from JSON body
          response_body = response_dict['results']
          #return the count of entries in JSON body as string
          lengthasstring = str(len(response_body))
          return lengthasstring
      
      def get_full_name(dictRequest):
          itemCount = int(dictRequest['count'])
          #parse response as json
          response_dict=json.loads(dictRequest['response'])
          # Create list from JSON body
          response_body = response_dict['results']
          #Extract values to return
          return response_body[itemCount]['name']['first'] + " " + response_body[itemCount]['name']['last'] 
      
      def get_location(dictRequest):
          itemCount = int(dictRequest['count'])
          #parse response as json
          response_dict=json.loads(dictRequest['response'])
          # Create list from JSON body
          response_body = response_dict['results']
          #Extract values to return
          return response_body[itemCount]['location']['city'] + ", " + response_body[itemCount]['location']['state']
    3. In the Python runtime version field, retain the default value as 3.
  10. From the Actions pane, click Python script > Execute function and drag it into the canvas below the Python script > Open action.
    1. In the Python session field, retain Default.
    2. In the Enter the name of function to be executed field, enter get_node_count.
    3. In the Arguments to the function drop-down list, select dResponseBody - String.

      python-execute
    4. In the Assign the output to a variable drop-down list, select sTotalUserCount - String.
  11. From the Actions pane, click Dictionary > Put and drag it into the canvas below the Python script > Open action.
    1. In the Dictionary variable field, select dRetrieveValue -Dictionary.
    2. In the Associate to this key field, enter response.
    3. In the New value drop-down list, select dResponseBody - String.
    4. In the Assign previous value to a variable drop-down list, select prompt-assignment - String.

      python-dict-open
  12. From the Actions pane, click String > To number and drag it into the canvas below the Dictionary > Put action.
    1. In the Enter the string field, enter $sTotalUserCount$.
    2. In the Assign the output to a variable drop-down list, select nTotalUserCount - Number.
      python-string-tonum
  13. From the Actions pane, click Loop > Loop.
    1. In the Loop Type > Iterator, select For n times from the drop-down list.
    2. In the times field, enter $nTotalUserCount$.
    3. In the Assign the current value to a variable drop-down list, select nCurrentUser - Number.
      python-loop
  14. From the Actions pane, click Number, select Decrement and place it inside the Loop action.
    1. In the Enter number field, enter $nCurrentUser$.
    2. In the Enter decrement value field, enter 1.
    3. In the Assign output to a variable drop-down list, select nCurrentUser - Number.
      python-num-decrement
  15. From the Actions pane, click Number, select To string and place it inside of the Loop action, below Number > Decrement.
    1. In the Enter a number field, enter $nCurrentUser$.
    2. In the Enter number of digits after decimal field, enter 0.
    3. In the Assign output to a variable drop-down list, select sCurrentUser - String.
      python-num-tostring
  16. From the Action pane, click Dictionary > Put and place it inside of the Loop action.
    1. In the Dictionary variable field, select dRetrieveValue -Dictionary.
    2. In the Associate to this key field, enter count.
    3. In the New value drop-down list, select sCurrentUser - String.
    4. In the Assign previous value to a variable drop-down list, select prompt-assignment - String.
      python-dict-put
  17. From the Actions pane, click Python script > Execute function and place it inside of the Loop action.
    1. In the Python session field, retain Default.
    2. In the Enter the name of function to be executed field, enter get_full_name.
    3. In the Arguments to the function drop-down list, select dRetrieveValue - Dictionary.
    4. In the Assign the output to a variable drop-down list, select sFullName - String.
      python-script-execute
  18. From the Actions pane, click Python script > Execute function and place it inside of the Loop action.
    1. In the Python session field, retain Default.
    2. In the Enter the name of function to be executed field, enter get_location.
    3. In the Arguments to the function drop-down list, select dRetrieveValue - Dictionary.
    4. In the Assign the output to a variable drop-down list, select sLocation - String.
      python-script-execute-location
  19. From the Actions pane, click Message box and place it inside of the Loop action.
    1. In the Enter the message to display field, enter the following:
      Full Name: $sFullName$ Location: $sLocation$
    2. Select Close message box after > Seconds, enter 5.
      python msgbox
  20. From the Actions pane, click Python script > Close and place it outside of the Loop action.
    1. In the Python session field, retain Default.
      python close
  21. Click Save to save your bot , and then click Run.
    The bot runs, displaying five full user names with locations for approximately 5 seconds each before completing its successful execution.

If you get a Bot Error message, see the following sections for a solution: Getting Bot Error while executing Python Script/Function (A-People login required), Getting Bot Error or True value in message box while executing Python Script (A-People login required), or An unexpected error occurred while executing Python Script command; Error code: bot.execution.error (A-People login required).