BACK

Using the JSON Package | #AAillustrates

Using the JSON Package

Most REST Web Services (APIs), some configuration files, and many NoSQL DB’s have something in common – they all use JSON. JavaScript Object Notation (JSON) is a lightweight, extensible format for storing, transporting, and retrieving data. JSON is designed to be flexible, easy to understand – using key-value pairs of data – and is often used when data is sent from a server to a webpage (when the page you’re reading loaded for example). In this tutorial, we’ll take a look at using the built-in Automation 360 JSON package read JSON values and arrays from a REST Get response.

The video tutorial above and text below makes reference to the following resources:

Start Session

The Start Session action enables developers to establish a session with a .JSON file or as Text (think REST payload response body). This establishes a JSON session, which can be referenced in the Get node list and Get node value actions. Note that this session can be of local or global scope:

  • Local Session Scope means that the session only exists within the existing bot, and any references to this session name outside of this bot would be invalid.
  • Global Session Scope means that the session can be referenced in this bot and in subtasks providing that the session is passed as an input value to a referenced task.

Get Node Value

The Get Node Value action enables developers to fetch JSON values by providing a node name/key or path for a corresponding session.

Example: Using the following JSON as a reference…

  • A JSON node key or path value of apiVersion would return a String value of 1.0.0.1
  • A JSON node key or path value of userCount would return a String value of 5
    • Non-string data is returned as String in this package
  • A JSON node key or path value of users[0].firstName would return a String value of Karen
    • JSON Array values can be accessed by providing an index in the path
  • A JSON node key or path value of users.length() would return the length of the users array – a String value of 5
{
    "userCount": 5,
    "apiVersion": "1.0.0.1",
    "lastUpdated": "2022-05-04T18:37:16.611Z",
    "emailVerified": true,
    "passwordSet": true,
    "questionsSet": false,
    "enableAutoLogin": false,
    "users": [
        {
            "userId": 1,
            "firstName": "Karen",
            "lastName": "Stephens",
            "phoneNumber": "310-940-7009",
            "emailAddress": "karen.stephens@automationanywhere.com",
            "jobTitle": "Program Director"
        }
    ]
}

Get Node List

Similar to the Get Node Value action, the Get Node List action has an input of JSON node key/name or path as well as a requirement for a valid session. This action is designed to be used when the provided JSON includes an array. This could be an array of String/Number/Boolean data or an array of JSON objects.

Example: Using the following JSON as a reference…

  • A JSON node key or path value of lastModifiedUsers would return a List of String values of:
    • 1
    • 3
    • 5
  • A JSON node key or path value of users would return a List of String values of:
    • {“userId”:1,”firstName”:”Karen”,”lastName”:”Stephens”,”phoneNumber”:”310-940-7009″,”emailAddress”:”karen.stephens@automationanywhere.com”,”jobTitle”:”Program Director”}
    • {“userId”:2,”firstName”:”Greg”,”lastName”:”Daniels”,”phoneNumber”:”704-974-7008″,”emailAddress”:”Greg.Daniels@automationanywhere.com”,”jobTitle”:”Sr. Developer”}
    • etc…not typing them all out here
    • Note: Because each returned list item is a JSON object, you could loop through the list – creating a new JSON session with each iteration and read the values out using the Get Node Value action as shown above.
  • A JSON node key or path value of users[*].firstName would return a List of String values of:
    • Karen
    • Greg
    • Vibhu
    • etc…not typing them all out here
{
    "userCount": 5,
    "apiVersion": "1.0.0.1",
    "lastUpdated": "2022-05-04T18:37:16.611Z",
    "emailVerified": true,
    "passwordSet": true,
    "questionsSet": false,
    "enableAutoLogin": false,
    "lastModifiedUsers" : [ 1, 3 , 5],
    "users": [
        {
            "userId": 1,
            "firstName": "Karen",
            "lastName": "Stephens",
            "phoneNumber": "310-940-7009",
            "emailAddress": "karen.stephens@automationanywhere.com",
            "jobTitle": "Program Director"
        },
        {
            "userId": 2,
            "firstName": "Greg",
            "lastName": "Daniels",
            "phoneNumber": "704-974-7008",
            "emailAddress": "Greg.Daniels@automationanywhere.com",
            "jobTitle": "Sr. Developer"
        },
        {
            "userId": 3,
            "firstName": "Vibhu",
            "lastName": "Soni",
            "phoneNumber": "415-780-5468",
            "emailAddress": "vibhu.soni@automationanywhere.com",
            "jobTitle": "SVP, Channel Sales"
        },
        {
            "userId": 4,
            "firstName": "Sarah",
            "lastName": "Nielson",
            "phoneNumber": "415-784-8453",
            "emailAddress": "sarah.nielson@automationanywhere.com",
            "jobTitle": "Lead, Dev Ops"
        },
        {
            "userId": 5,
            "firstName": "Jignesh",
            "lastName": "Talwari",
            "phoneNumber": "910-745-4687",
            "emailAddress": "jignesh.talwari@automationanywhere.com",
            "jobTitle": "Marketing Director"
        }
    ],
    "tenantUuid": "823eaadd-363a-4384-a1f3-5fcb6d32d5b7"
}

End Session

The End Session is actually pretty important in this package. If you’re JSON has an array of JSON objects, it common to create nested JSON sessions (with different session names of course) to iterate through them. Just make sure you actually end the session in between each iteration of your loop, or your bot will error out indicating its having issues creating a new session because a session already exists by that name. So key takeaway for this package (and any package that uses sessions): If you start a session – be sure you also end that same session.

Conclusion

If your Automation 360 bot is interfacing with any REST API’s, it’s quite common that you’ll run into a JSON-formatted response body…but have no fear: armed with the JSON package and some of the examples provided above, you’ll be able to parse and extract values like a pro – to get out the data you need in order to successfully automate your process. Not using REST? JSON files can be great for storing and retreiving configuration data that your bot may reference in determining server/environment-specific details, folder share details, etc. In either case, explore how the JSON can accelerate your bot building and help you meet your automation objectives.