Creating Pojo classes in Python
By: Kumar Gaurav
Tags: Pojo, Json, Python, Parsing

Preview 1.1

So Today I am gonna talk about creating Pojo classes in Python.

If you’re a Java/Android Programmer, you must be knowing how important these pojo classes are to parse json/xml responses; the responses may be coming from server or local databases etc.

In Java/Android we use well known libraries like Gson or Jackson or Mochi. These libraries are helpful in serializing and deserializing the responses.

For Example if we have a response like this:

{“name”: “Uzumaki Naruto”, “village”: “Leaf Village”}

In Java for above JSON, we will create a Pojo class which would look something like this:

Snippet 1.1 A Java class representation for User data

As we can see in above code snippet json keys are annotated with SerializedName. Similar thing in Python is bit tricky but easier at the same time.

Before jumping on how to do the same in Python, Let’s know about two apis:

1. Dictionary, and

2. **kwrgs

A Dictionary is nothing but an unordered collection which works very similar like json. Same above json example can be re-written in Python dictionary like below:

Snippet 1.2 example of dictionary in Python

I won’t go into more details but I think now we have at least basic idea about dictionary. But to explore we can always head to Official Site. And **kwargs allow you to pass multiple keyword arguments to a function. To understand **kwargs; we will look at one example. Let’s run the below code snippet which can accepts any number of named arguments.

Snippet 1.3 demo on **kwargs Usages

The output of this method should return:

RealPythonIsGreat!

As we can see we passed few named arguments i.e. a,b, c, d and e. and it printed without any error, Why? Because there is no constraint on which named arguments it is looking for specifically. So we can pass any named arguments. Now let’s constraints above method with only two named arguments; like below:

Snippet 1.4 demo on using kwargs not correctly

Running above program will throw standard runtime error, since expectation was 2 named arguments while we passed more than two. Here is the error log:

So we can understand from above example how does this kwargs works. Now we will use these two APIs for creating Pojo classes.

Let’s create a class for json example we took above with the variables named after the keys present. Snippet of the class code is here:

Snippet 1.5 A Python class representation for User data

Now we will create couple of helper methods to convert dict to pojo and vice-versa.

Snippet 1.6 Helper class to serialize and deserialize objects

As we can see in the method user_from_dict(), we are returning User(**s) where **s is a kwargs arguments. But notice the constructor of User class, it expects only two named variables i.e. name and village. So we will pass only a dictionary which will have only these two key-value pairs. Let’s use these two helper methods with above example:

Snippet 1.7 Using helper methods

If we will execute above code; it’ll return below output:

Uzumaki Naruto

Leaf Village

Look what Peppa is saying about “creating Pojo classes in Python” !

I hope now we all can agree with this, right? :-)