Mapping JSON Data Using Objects
It is quite common that you have to do some data mapping when you create an integration between two systems. It is possible to hardcode the JSON string into the Task which will send the data or store the data, but in some cases the JSON string format depends on the data that is given to the Process as an input and hardcoding the format is not an option. For example, you need to use different keys depending on some rules or you need to create a JSON array whose amount of JSON data entries varies depending on the input data. In these kinds of scenarios, JObjects and JArrays are great tools to process the input data into the JSON data that we want. In this module we will look into different ways of mapping JSON data using objects.
Creating new JObject
Below we have a JSON data example that we will be mapping in this module.
Let's say that we want to map this data into new JSON containing employer information. We want to change the keys we have in the input data and add some new keys. To achieve this, we can create a new JObject to which we map the data we want to create. First, we need to parse the input data into JObject so we can easily access the input values. In this example we will use the JObject.Parse-method.
We can then use a C# Statement Element to create a new JObject where we will map the input data. A great feature of a JObject is that we don't have to introduce all the properties of the object when we create the object, but we can dynamically add new properties to the object if we define the variable containing the object as dynamic. In the below example we combine "Firstname" and "Surname" into "FullName" and we add a new property "CostUnit" to which we save the unit in which the person is working in. We then save this new JObject into the "mappedJson" variable. Note that when we create the "FullName" property, we are using string interpolation and not Handlebars which are used when accessing references in text.
We can then call ToString-method to transform the created JObject into a JSON string. Below is an example of calling the method and the result that the call produces.
Creating new JArray
Sometimes we get multiple JSON entries in one JSON input, and we need to map all of those into a new JSON array which will be sent to some other system or saved to a database. In this case we can use a JArray to which we will store all new mapped JObjects. Let's say we have the following JSON which we want to map in the same way as we did in the previous example.
To easily access this data, we need to parse this input JSON into a JArray using a Frends.Json.ConvertJsonStringToJToken Task or by calling the JArray.Parse method in a C# Expression Element. In this example we will be calling the JArray.Parse-method in a C# Expression Element.
After parsing the data into a JArray we are able to iterate through the data and do the mapping for each of the entries. Before implementing the iterator, we need to create a new object from the JArray class so we are able to add JObject objects to the array. We can do this using a C# Expression Element.
We can then use the Foreach Element to iterate through the input JSON which is now converted into a JArray.
In the iteration we can use a C# Statement Element to create new JObject as we did in the previous example. The difference between the previous example and this scenario is that instead of returning the created JObject, we will add the JObject to the JArray which we introduced before the iteration.
After iteration, we can call the ToString method to create JSON string from the JArray, which we then can pass to other Elements as a parameter. Below is an example of the call and the result of data mapping.
Creating JSON from object
In some cases, you might want to create JSON from an object. For example, you get a result from some Task, and you want to create JSON from that object. To achieve this, we can create a JObject from the object using the JObject.FromObject-method. This method will take the properties of the object and transforms them into JObject properties. We can then use the ToString-method to transform the JObject to JSON string.
Let's take a look at an example of the above case. First, we can create an anonymous object which contains properties that we have previously passed as JSON to the Process.
We can then call the JObject.FromObject-method to transform this anonymous object into a JObject.
Once we have created the JObject we can call ToString-method to create JSON string from the JObject. Below is an example of the call and the result it produces.
Adding keys dynamically using Dictionary
Sometimes it is not enough to map data into hardcoded JSON keys, but you also need to dynamically create the keys and map data to them. This can be done using a Dictionary. In a nutshell, a Dictionary is a collection of key-value pairs. As the name suggests, it's similar to any real life dictionary, such as a Finnish-English-Finnish language book. We can use this data type to create key-value pairs and then we can create a JObject from the Dictionary same way as in previous example, using JObject.FromObject-method.
Lets take the previous example, where we created a JArray containing multiple JObjects. Steps will be the same as in that example, but lets modify the step where we created a new JObject and added it to the JArray. This time we will be using Dictionary instead of JObject. We can add the same "FullName" and "CostUnit" keys, but now we will also add a property containing information about which age group the person belongs to. The property will be determined according to the person's age. First we will determine the age group key using if-statement and then we will add the key-value pair to the Dictionary. Lastly we will create a JObject from the Dictionary and add it to the JArray.
We can then call JArray.ToString-method to create JSON string from the JArray to see how the dynamic mapping of keys and values has created age groups.
The next article is Introduction to Using LINQ with JSON Objects