Solution: Conversion of Python Dictionary / to .Net data type. ( Get Python Object Activity )

As we all know that Python code integration is available from UiPath just by using the Python Scope activity, which is available in UiPath.Python.Activities package. If it is not already available in the studio then we can load it by using the Manage Packages tool under the DESIGN menu from the top section of the studio.

You can read more about this UiPath.Python.Activities package here

So, by using this activity it is very easy to execute the Python code and get the result from your workflow or sequence. There would not be any problem in the conversion of Python’s result Data Type to .Net Data Type if it is an integer, string, etc.

The problem occurs in conversion for data type such as the Dictionary ( which holds different data types data as its element. ) from the Python and we need to convert into the appropriate .Net Data Type.

So, let see the solution for the same.

  • Do not return the Dictionary object directly from the Python, instead of that return JSON data from the Python script.
  • To return JSON from Python script import json library in your code.
  • Use the dumps method from this json object.
  • To read more about this Python JSON encoder and decoder use this link.

Here is the sample code returning Dictionary object as a JSON from the Python script.

return json.dumps(
        {
            'email': [
                'somexample@somedomain.com',
                'anotheremailaddress@somedomain.com'
            ],
            'contact_numbers': [
                '12345 67890',
                '09876 54321',
                '1234578690'
            ]
        }
    )

Now, we can send Python’s Dictionary object as JSON. The second step is we have to use this in our UiPath studio. We can easily Deserialized by using the activity Deserialize JSON from Uipath.Web.Activities which results in a JsonObject.

If this Uipath.Web.Activities do not exist in the studio then we can add in the same way as we have added the UiPath.Python.Activities package via Manage Packages menu.

Once JsonObject is received, it is easy to iterate by using the For Each activity.

I have attached the sample sequence and a Python file.
SimplePythonCode.zip (14.8 KB)

Note:

  • Python needs to be installed in your machine.
  • Install Uipath.Web.Activities and Python Scope activity packages before executing the sequence.

I hope this could help someone. If anyone knows a better approach than this then please let me know in the comments.

3 Likes