Handling Complex Objects in K2 SmartObjects

Deserializing Complex Objects in SmartObjects

java-serialization.png

This post is in continuation to my previous post K2 Integration with REST Api Services where I’ve created an SMO for a REST Api. As api’s return you serialized response, we need to further deserialize them using the Deserialize methods of that particular type/object.

Now if you see the result in below screen shot which is the result of WeatherAPI, it is providing a Serialized objects for Clouds, Coord, Main, Sys. Now to get the individual properties of the above mentioned serialized types/objects, we need to further deserialize them. Lets see how we can do.

SMOResults.jpg

Now the SMO which we have created in last post needs to be modified using K2 Designer. Select the method from service instance (Here it is GetWeatherbyCityName) and map the properties as shown in below screen shots.

ServiceInstance.jpg

MainMethodMapping.jpg

Now if you see the mapped properties in above screen shot, Clouds is mapped to a memo property Clouds (Clouds) which is the serialized result from api. Once the method is created click add and select Deserialize method of clouds from service instance as shown below.

CloudsDesrialize.jpg

Now the Deserialize method takes the serialized value as input and returns the individual properties of Clouds type. You need to map the serialized value which we got from above method as input and map the individual return properties to SMO properties (Here the property is ALL).

DeserializeClouds.jpg

Now similar to above add deserialize methods of other class types (Coord, Main..) and map their respective properties.

AllMethods

Now once you finish,  it should like below.

Method.jpg

That’s it. Click Finish and execute the SMO to see the individual properties as below

 

Results

CloudsALL is the deserialized value from Clouds Object, Similarly CoordinatesLatitude, CoordinatesLongitude are the desreialized values from Coord object, similarly Main Object Values you can see there.

That’s it. This is how we can Deserialize complex objects.

Here we have seen only single level of complexity which means for every weather result object, we get only one cloud object, one Coord object and so on. But there could be cases of result with multi-level complexity which means for every single weather result object there could be multiple cloud objects or multiple Coord objects and so on. In that case we may not be able to deserialize as we did above. We need to create separate methods for each object like GetCloudsbyCityName, GetCoordbyCityName, GetMainValuesbyCityName and handle the methods in same way as we did above.

Also in some cases, Api methods takes serialized objects as an input values, in that case we need to use the serialize method of that objects first which returns us the serialized value and then we need to pass this serialized value to the main method (just opposite to the way we did above).

This is how we can handle complex objects in K2 Smartobjects.

 

Author: Vijay

I'm a K2 Certified Developer, helping teams to automate, design, develop & maintain business process applications connecting across multiple platforms with multiple technologies.

8 thoughts on “Handling Complex Objects in K2 SmartObjects”

  1. Hi Vija,
    For the REST API which returns multi-level complexity type/objects, as you have mentined
    “We need to create separate methods for each object like GetCloudsbyCityName, GetCoordbyCityName, GetMainValuesbyCityName and handle the methods”

    you are refering to create these methods as part of REST Api services right?

    For one of my project i do want integrate with multi-level complex types, need to understand this a bit more.

    Thanks,
    Nanda Kumar.K

    Like

    1. Hello Nanda Kumar,

      Multilevel deserialization is not possible in K2 as of now.. if possible send the seralized output of your service to me, i’ll verify once and confirm whether we can deserialize or not

      Regards,
      VJ.

      Like

      1. PFB for the example rest service output;

        Please reply if there is way to de-serialize this kind of data OOTB in K2 Five or latest version.

        An example of my REST service return type is below;
        {
        “Result”: {
        “Id”: 7872,
        “Name”: “Transaction_Name_7872”,
        “Types”: [
        {
        “Id”: 1722,
        “Name”: “Type_Name_1722”,
        “MetadataValues”: [
        {
        “Id”: 1723,
        “Name”: “1722”,
        “Value”: “Values”
        },
        {
        “Id”: 1724,
        “Name”: “1722”,
        “Value”: “Values”
        }
        ]
        },
        {
        “Id”: 33579,
        “Name”: “Type_Name_33579”,
        “MetadataValues”: [
        {
        “Id”: 33580,
        “Name”: “33579”,
        “Value”: “typeSecond_Values”
        },
        {
        “Id”: 33581,
        “Name”: “33579”,
        “Value”: “typeSecond_Values”
        }
        ]
        }
        ]
        },
        “ResultCode”: 200
        }

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s