Deserialize exported Trello JSON to C# objects

Trello JSON to C# objects

Trello is an excellent issue management system, and much more. We use it for project management and allow transparency of operations to our customers. Exporting of Trello data is possible, and the output of the data is in JSON format. While JSON is easy to understand, data analysts require the data in a format that is easier for them to understand – such as CSV, Excel, database table(s) etc. This article describes how conversion of Trello JSON to C# objects helps with formatting Trello data with C#.

I have built a prototype project to take the exported JSON file from Trello and save the data in C# objects. This process is called deserialization from JSON to C# objects. The C# objects can then be serialized (converted) to a CSV, Excel, database table(s) etc. I have only implemented deserialization in this project. Feel free to get the source code from GitHub. An advanced tool called Trello.NET is available on GitHub also.

Trello JSON to C# objects

Trello JSON

The exported file from Trello looks something like this:

Deciphering Trello JSON

Let’s break this information into pieces for easier understanding:

This is the root of information. It means, is export file has property called ID, Name and other such properties. To represent this information, our object will look like this:

Some properties in the root can be complex. For example:

Some more details

This means the root has an object called prefs with its own properties such as PermissionLevel, Voting etc. To allow deserialization of nested Trello JSON to C# objects, we create a class for pref. We also modify our root Trello class.

There are other objects that add more complexity. For example:

Hierarchy of Trello JSON

This means that root has an object called Memberships, which in turn has its own properties such as ID, IdMember etc. Notice that these properties are within squared brackets [{comma separated properties}, {comma separated properties}, …]. This indicates that Membership may have multiple objects with its own properties. At this point the hierarchy looks something like this:

Deserializing JSON

To deserialize Trello JSON to C# objects with this nesting and complexity, we will add a class for Membership and adjust our root Trello class appropriately.

Notice that root Trello class accommodates multiple memberships using a List of Membership. To add to this complexity, we have cards. The root has a cards object. There can be one or more cards. Each card can have one or more properties and objects. Let’s look at the cards JSON object.

Additional breakdown

Notice that there can be one or more cards. Each card has property ID. It also has an object called CheckItemStates. Card can have one or more CheckItemStates. Each CheckItemState has 2 properties and the hierarchy looks like this:

This level of JSON nesting can also be handled by building appropriate classes like so:

Serialize data into objects

Great. Now we have classes set up just like the JSON is set up. We will use an object called JavaScriptSerializer() from System.Web.Script.Serialization namespace. Add a reference to System.Web.Extensions to get access to the serialization namespace. Notice how easy it is to allow JavaScriptSerializer to parse JSON to our objects.

That’s all there is to serializing Trello JSON to C# objects. You could write the data from objects to a CSV file, if you like. You could store the data in database table(s), or build your own workflows.

If you would like to ingest only a few properties from JSON, you could just remove unnecessary properties from your classes and JavaScriptSerializer will completely ignore those properties in JSON to which you don’t have any mapped properties in your objects.

Hopefully this code set will help deserialize Trello JSON to C# objects (POCO) and serialize the data to a format that is easier for you to analyze. Feel absolutely free to fork/extend the examples and share on GitHub.