Build a bot that runs a JSON script with multiple records. In this
example, we use a JSON file that contains a list of feeds from a blog, and the bot extracts the required information from the JSON file and prints it to a
message box.
Prerequisites
To run JSON script from
Automation 360, you
must already have the JSON data stored either as a variable or a file (.json, .txt
format) on your desktop. The following screenshot displays the structure of a JSON
object as a sample.
{
"feeds": [
{
"id": 1090,
"title": "Amsterdam Beaches",
"description": "It’s true that few people think of the Netherlands when planning a beach holiday. But whether you’re just visiting or lucky enough to call this iconic city home, there are plenty of great beaches to enjoy in Amsterdam",
"location": "Kloveniersburgwal 1, 1012 CW Amsterdam, Netherlands",
"lng": 0,
"lat": 0,
"userId": 3043,
"name": "Ritu singh",
"isdeleted": false,
"profilePicture": "/Images/userimageicon.png",
"videoUrl": null,
"images": null,
"mediatype": 0,
"imagePaths": null,
"feedsComment": null,
"commentCount": 3,
"multiMedia": [
{
"id": 2157,
"name": "beach of amsterdam.PNG",
"description": null,
"url": "feedsmedia/3043/c5522956-4ab5-408e-b540-16a8c90af9b0.PNG",
"mediatype": 1,
"likeCount": 0,
"place": null,
"createAt": "0001-01-01T00:00:00"
}
],
"likeDislike": {
"likes": 2,
"dislikes": 1,
"userAction": 2
},
"createdAt": "2019-12-26T07:08:38.4659686",
"code": 0,
"msg": null
}
],
"totalFeed": 125
}
Procedure
-
Create a bot.
-
On the left panel, click Automation.
A list of available bots and forms is
displayed.
-
Click .
-
Provide a name for the bot:
JsonBot.
-
Click Create and Edit.
-
Start a JSON session by using the
action.
-
From the Actions pane, double-click or drag the
action.
-
In the Data Source field, choose
File and select the Desktop
file file type. Click Browse to
find your JSON file, for example, D:\userdefined\My
Downloads\Sample Json with 200 Records.json.
-
To create a JSON session, from Json object
session, select Local session and
enter Default as the session name.
-
Create a variable:
SampleString
-
Type: String
-
Name: SampleString
-
Retrieve the value of the total number of feeds from the JSON script by using
the
action.
-
From the Actions pane, double-click or drag the
action.
-
In the JSON node key or path field, enter
totalFeed.
-
In the Session name field, enter
Default.
-
In the Save the outcome to a variable field,
select SampleString.
-
To print the value of totalFeed, double-click or
drag the Message box
action.
-
To open the list of your variables, in the Enter the message
to display field, press F2, and then select
SampleString.
-
Click Yes, insert.
-
To retrieve any specific value from 125 feeds, perform the following
steps.
-
From the Actions pane, double-click or drag the
action.
-
In the JSON node key or path field, enter
feeds[52].title.
-
In the Session name field, enter
Default.
-
In the Save the outcome to a variable field,
select SampleString.
-
To print the value of feeds[52].title,
double-click or drag the Message box
action.
-
To open the list of your variables, in the Enter the message
to display field, press F2, and then select
SampleString.
-
Click Yes, insert.
-
Create a variable:
StringList
-
Type: List
-
Subtype: String
-
Name: StringList
-
To retrieve the list of child nodes from a node path, use the
action.
-
From the Actions pane, double-click or drag the
action.
-
In the JSON node key or path field, enter
feeds.
-
In the Session name field, enter
Default.
-
In the Save the outcome to a variable field,
select StringList.
-
To print all the child nodes under the
feed 51
object,
double-click or drag the Message box
action.
-
To open the list of your variables, in the Enter the message
to display field, press F2, and then select
StringList[51].
-
Click Yes, insert.
-
To close a JSON session, use End session. In the
Session name field, enter
Default.
-
Click Save and then click
Run.
-
Start a new JSON session.
-
From the Actions pane, double-click or drag the
action. In the Data Source field,
select Text and enter
StringList[52].
-
From Json object session, select
Local session and enter Default
1 as the session name.
-
To retrieve the value from a specific node, use .
-
From the Actions pane, double-click or drag the
action.
-
In the JSON node key or path field, enter
description.
-
In the Session name field, enter
Default 1.
-
In the Save the outcome to a variable field,
select SampleString.
-
To print the value of the description node,
double-click or drag the Message box
action.
-
To open the list of your variables, in the Enter the message
to display field, press F2, and then select
SampleString.
-
Click Yes, insert.
-
To close the JSON session, use End session. In the
Session name field, enter Default
1.
-
Click Save and the click
Run.
You can further perform operations to extract the required information from
the JSON feed and use the extracted values in your bot. To
extract the name value from feed 50 through 60, perform the following
steps.
-
Create a variable: ListItem
-
Type: String
-
Name: ListItem
-
To process each item in the list, use the Loop
action.
-
Double-click or drag the Loop
action.
-
Select the For each item in the list
iterator.
-
In the List field, select
StringList.
-
Choose the Range option and for the following
fields, enter the values provided:
-
From Index:
50
-
To index: 60
-
In the Assign the current value to variable
field, select ListItem.
-
Start a new JSON session.
-
From the Actions pane, double-click or drag the
JSON > Start session
action.
-
In the Data Source field, select
Text and enter
ListItem.
-
To create a JSON session, from Json object
session, select Local session and
enter Default 2 as the session name.
-
To retrieve the value from a specific node, use the
action .
-
From the Actions pane, double-click or drag the
action.
-
In the JSON node key or path field, enter
name.
-
In the Session name field, enter
Default 2.
-
In the Save the outcome to a variable field,
select SampleString.
-
To print the value of the name node,
double-click or drag the Message box
action.
-
To open the list of your variables, in the Enter the message
to display field, press F2 , and then select
SampleString.
-
Click Yes, insert.
-
Click Save and then click
Run.
The name values retrieved from the feeds 50 through 60 are as follows:
Feeds (object) |
Name |
50 |
Ashish |
51 |
Sophia |
52 |
Ritu |
53 |
Shivam |
54 |
Navya |
55 |
Faiz |
56 |
Martin |
57 |
Shweta |
58 |
Jagjit |
59 |
Mark |
60 |
Sophia |