Using Python to parse JSON response
- Updated: 2023/04/05
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.
Do one of the following:
- While installing the Python, make sure to select Add
Python x.xx to PATH during the Python
installation.
-
Add a path to the Environment variable.
- While installing the Python, make sure to select Add
Python x.xx to PATH during the Python
installation.
Procedure
- Log in to the Control Room.
-
On the left pane, click Automation.
A list of available bots and forms is displayed.
-
Click Create a bot icon.
- In the Name field, enter PythonTutorial.
- Click Create & Edit.
-
In the Actions pane, click Variables to create the
following new variables:
- Name dResponse, Type > Dictionary and Subtype > String.
- Name dRetrieveValue, Type > Dictionary and Subtype > Any.
- Name dResponseBody, Type > String.
- Name sFullName, Type > String.
- Name sLocation, Type > String.
- Name sTotalUserCount, Type > String.
- Name nTotalUserCount, Type > Number.
- Name nCurrentUser, Type > Number.
- Name sCurrentUser, Type > String.
- Name prompt-assignment, Type > String.
-
From the Actions pane, select REST Web Services > Get method and place it under the Start of the bot flow.
-
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.
-
In the Authentication Mode drop-down list,
select No Authentication.
- In the Assign the output to a variable drop-down list, select dResponse -Dictionary of Strings.
-
In the URI field, enter https://randomuser.me/api/?results=5&inc=name,email,location&nat=us.
-
From the Actions pane, click String > Assign action and drag it into the canvas below the REST Web
Services action.
-
In the Select the source string variable value
field, enter $dResponse{Body}$.
- In the Select the destination string variable drop-down list, select dResponseBody - String.
-
In the Select the source string variable value
field, enter $dResponse{Body}$.
-
From the Actions pane, click Python script > Openand drag it into the canvas below the String > Assignaction.
-
In the Python field, select Manual
input.
-
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']
- In the Python runtime version field, retain the default value as 3.
-
In the Python field, select Manual
input.
-
From the Actions pane, click Python script > Execute function and drag it into the canvas below the Python script > Open action.
- In the Python session field, retain Default.
- In the Enter the name of function to be executed field, enter get_node_count.
-
In the Arguments to the function drop-down list,
select dResponseBody - String.
- In the Assign the output to a variable drop-down list, select sTotalUserCount - String.
-
From the Actions pane, click Dictionary > Put and drag it into the canvas below the Python script > Open action.
- In the Dictionary variable field, select dRetrieveValue -Dictionary.
- In the Associate to this key field, enter response.
- In the New value drop-down list, select dResponseBody - String.
-
In the Assign previous value to a variable
drop-down list, select prompt-assignment -
String.
-
From the Actions pane, click String > To number and drag it into the canvas below the Dictionary > Put action.
- In the Enter the string field, enter $sTotalUserCount$.
-
In the Assign the output to a variable drop-down
list, select nTotalUserCount - Number.
-
From the Actions pane, click Loop > Loop.
- In the Loop Type > Iterator, select For n times from the drop-down list.
- In the times field, enter $nTotalUserCount$.
-
In the Assign the current value to a variable
drop-down list, select nCurrentUser -
Number.
-
From the Actions pane, click Number, select
Decrement and place it inside the
Loop action.
- In the Enter number field, enter $nCurrentUser$.
- In the Enter decrement value field, enter 1.
-
In the Assign output to a variable drop-down
list, select nCurrentUser - Number.
-
From the Actions pane, click Number, select
To string and place it inside of the
Loop action, below Number > Decrement.
- In the Enter a number field, enter $nCurrentUser$.
- In the Enter number of digits after decimal field, enter 0.
-
In the Assign output to a variable drop-down
list, select sCurrentUser - String.
-
From the Action pane, click Dictionary > Put and place it inside of the Loop
action.
- In the Dictionary variable field, select dRetrieveValue -Dictionary.
- In the Associate to this key field, enter count.
- In the New value drop-down list, select sCurrentUser - String.
-
In the Assign previous value to a variable
drop-down list, select prompt-assignment -
String.
-
From the Actions pane, click Python script > Execute function and place it inside of the Loop
action.
- In the Python session field, retain Default.
- In the Enter the name of function to be executed field, enter get_full_name.
- In the Arguments to the function drop-down list, select dRetrieveValue - Dictionary.
-
In the Assign the output to a variable drop-down
list, select sFullName - String.
-
From the Actions pane, click Python script > Execute function and place it inside of the Loop
action.
- In the Python session field, retain Default.
- In the Enter the name of function to be executed field, enter get_location.
- In the Arguments to the function drop-down list, select dRetrieveValue - Dictionary.
-
In the Assign the output to a variable drop-down
list, select sLocation - String.
-
From the Actions pane, click Message box and place it
inside of the Loop action.
-
In the Enter the message to display field, enter
the following:
Full Name: $sFullName$ Location: $sLocation$
-
Select Close message box after > Seconds, enter 5.
-
In the Enter the message to display field, enter
the following:
-
From the Actions pane, click Python script > Close and place it outside of the Loop
action.
-
In the Python session field, retain
Default.
-
In the Python session field, retain
Default.
-
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).