More

    How to deserialize JSON in UiPath?

    In this post, I will explain how to properly use the JSON activities in UiPath. We will cover in detail how to use the deserialize activities provided by UiPath. Before diving deep into UiPath activities for JSON we should understand what JSON is.

    What is JSON?

    Skip this section if you already have a good understanding of JSON.

    JSON stands for JavaScript Object Notation. It is an open-standard file format that uses human-readable data objects. A JSON string can contain key-value pairs and array data types. Because of its easy readability and ease of use, JSON has started service as a replacement for XML.

    JSON Data Types

    In JSON we can store values of the following data types

    1. string
    2. number
    3. another JSON object
    4. array
    5. boolean
    6. null

    JSON Objects

    • Curly braces {}surround a JSON Object.
    • JSON objects are represented as key-value pairs.
      • All keys should be strings.
      • Values could be any valid JSON data type.
      • Key and values should be separated by a colon :.
      • Key-value pairs should be separated by a comma ,`.

    Sample JSON Object

    {
        "site": "Aneejian",
        "URL": "https://aneejian.com",
        "postId": 32,
        "liked": true
    }

    JSON Arrays

    A JSON array is similar to arrays in other programming languages.

    • Square brackets []surround a JSON Array.
    • Array values could be any valid JSON data type.
    • JSON array can store multiple data types.
    • Values in a JSON Array should be separated by a comma ,.

    Sample JSON Array

    [
      "India",
      "Australia",
      "England"
    ]

    Handling JSON in UiPath

    Before getting started, we should set up UiPath.WebAPI.Activities activity in your UiPath project. You can find the activity under the Official (https://www.myget.org/F/workflow/) feed of UiPath.

    After installing you will be able to find the JSON activities from activities panel under Available >> Programming >> JSON

    The JSON activities in UiPath is comprised of two sub-activities.

    • Deserialize JSON to handle any JSON Object.
    • Deserialize JSON Array to handle any JSON arrays.

    Deserialize JSON Activity

    Deserialize JSON Activity deserializes JSON object strings. Curly braces {} surround a JSON object.

    To explain this, let us consider the following JSON object string. We can save it in a file named jsonObject.json

    {
        "site": "Aneejian",
        "URL": "https://aneejian.com",
        "postId": 32,
        "liked": true
    }

    Follow the steps below to read and deserialize the JSON Object string with UiPath. Save it to a .json file.

    • Read the JSON file using Read Text File activity and save the output to a variable jsonObjectString.
    • Now pass the jsonObjectString to Deserialize JSON Activity and save the output to a variable named jsonObjectData. Please note, the output data of Deserialize JSON Activity is a JObject.
    • So, now we have a variable jsonObjectData of type JObject.

    How to get values from a JObject?

    We can use the following assign activities to get the values.

    • To get sitewhich is of data type string
      • jsonObjectData("site").ToString
    • To get url which is again of datatype string
      • jsonObjectData.Item("url").ToString
    • To get postId which is of type integer
      • CInt(jsonObjectData.Item("postId"))
    • To get liked whose datatype is Boolean
      • Convert.ToBoolean(jsonObjectData("liked"))

    With the above code, we have got individual values of all keys.

    Deserialize JSON Array Activity

    This activity deserializes JSON Array strings. JSON Arrays are surrounded by square brackets [].

    To explain this, let us consider the following JSON Array string. Save it to a .json file.

    [
      "Raul",
      "Jasmine",
      "Susan"
    ]

    Follow the steps below to read and deserialize the JSON Array string with UiPath.

    Read the JSON file using Read Text File activity and save the output to a variable jsonArrayString.Now pass the jsonArrayString to Deserialize JSON Array activity and save the output to a variable named jsonArrayData. Please note, the output data of Deserialize JSON Array activity is a JArray. So, now we have a variable jsonArrayData of type JArray.

    How to get values from a JArray?

    There are multiple ways by which we can retrieve data from a JArray.

    • A For Each loop can iterate through each item in a JArray.
    • Get items by index.
      • jsonArrayData.Item(2) will return Susan.

    Deserializing a complex JSON

    Now let us see how to deserialize a mixed JSON with both JSON Objects and JSON Arrays in it.

    Sample JSON

    {
      "name":"John",
      "age":30,
      "cars": [
        { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
        { "name":"BMW", "models":[ "320", "X3", "X5" ] },
        { "name":"Fiat", "models":[ "500", "Panda" ] }
      ]
     }

    Once we get a JSON string to parse, we need to understand its structure.

    Let us say, we got the above JSON string from a website which represents car wishlist of their customers. From UiPath, we have to convert it to the following string.

    John, aged 30 has shortlisted 8 cars from 3 different brands.
    The cars shortlisted are Ford Fiesta, Ford Focus, Ford Mustang, BMW 320, BMW X3, BMW X5, Fiat 500, and Fiat Panda.

    To form the string above we need to identify the following parameters.

    • Name of the person.
    • Age of the person.
    • Cars array.
    • The number of brands he shortlisted.
    • The number of cars the person shortlisted.
      • This would be the sum of all models under each brand the person has shortlisted.
    • Name of each car with brand name prefixed.

    Let us see how we can do this.

    Step 1: Deserialize the base JSON string

    Since the string is surrounded by curly braces {}, it represents a JSON object. So, we have to use Deserialize JSON activity on the string.

    After using the deserialize activity, we will get the values of the following parameters. Let us assume we saved the output of activity into a variable named personData.

    • Person name by using personData("name").ToString
    • Age of the person by using personData("age").ToString
    • Cars array by using personData("cars").ToString

    Step 2: Deserialize the cars string

    Since the cars string is surrounded by square brackets [], it represents a JSON Array. So, we have to use Deserialize JSON Array activity on the string.

    After activity execution, we will have values of the following parameters.

    • Number of brands by using carsArray.Count
    • By iterating through the carsArray we can get details of the cars also. For that, we have to use Deserialize JSON on each item.

    Wait, is it that complex?

    Actually not. You can get values of all the parameters we require just by using the deserialization done at the first step.

    Here is how.

    We can find the number of car brands by using personData("cars").Count.ToString

    So, we can directly access the JArray object by using personData("cars").

    If we go further and use personData("cars").Item(0)("models").Count we will get the number of Ford cars.

    I believe you got the idea. Do the rest as an assignment. Please comment if you have any doubts or queries.

    Related Articles

    Recent Articles

    How to fix Date Time data type issue in Blue Prism?

    Know more about the date time issue while using the code stage in Blue Prism and understand how to fix it by dealing with the UTC offset.

    How to deserialize JSON in UiPath?

    Tutorial on how to properly deserialize JSON with UiPath Web Activities. Explains the concept of JSON Array and JSON Object.

    Dynamic or Variable Selectors in UiPath

    Tutorial on how to use dynamic selectors in UiPath. Explains how to use variables in UiPath selectors.

    Change Case Excel Add-In

    Change Case Excel Add-In is a powerful Excel Add-In that can change the case of selected cells. With easy to use Keyboard shortcuts, perform case conversion in style.

    Blue Prism Video Tutorial

    A well-structured video tutorial on Blue Prism, which is the right starting point for your RPA career. #rpa #blueprism

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Stay on top - Get latest articles in your inbox