Skip to main content

Introduction to JSON Objects

The basic concepts of JSON Objects

Ossi Galkin avatar
Written by Ossi Galkin
Updated over a year ago

JSON Objects

JSON Objects are object representations of JSON data. Each key-value pair in JSON data is stored as properties and values of the object. In Frends, the NewtonSoft.Json library is used to create objects from JSON data. In addition to storing JSON data in objects, you also get the benefits of Object-Oriented Programming, since JSON objects inherit methods and properties from the Object class. Objects also enable different ways of processing data efficiently compared to raw JSON data. In this module we will be looking into most used JSON objects in Frends.

JObject

The JObject class is an object representation of a single JSON object. This object contains properties which represent JSON keys. These properties have values which are representation of JSON key values. Lets take a look at the following example JSON data from a Frends C# Expression Element.

Here we have JSON data which has three keys: FirstName, SecondName and Age with values Erik, Example and 20. We save this value into a variable named "json". Behind the scenes this is a JSON string.

We can then parse this JSON string to a JObject in two ways: using a Frends.JSON.ConvertJsonStringToJToken Task or the static JObject class method Parse. With a ConvertJsonStringToJToken Task you can just pass the JSON string to the Task as a parameter and the result of the Task will be a JToken, which is a parent of the JObject class and behaves in mostly the same way as a JObject.

The second way is to use the static method JObject.Parse() in a C# Expression Element, in which you pass the JSON string as a parameter. You can then save the created JObject to a variable.

After creating the object, we can access the values, as with any other object, using dot notation.

Sometimes the JSON data contains special characters in the keys, such as whitespaces or slashes, which do not transform into object properties very well since they are illegal characters. In these cases, you can use brackets to access the data. Above is an example JSON data which contains a key named "Home Address".

Since the key contains a whitespace, you cannot use dot notation to access that data. You can however use brackets to access that key. After you have parsed the data using a Frends.JSON.ConvertJsonStringToJToken Task or the JObject.Parse method you can access the data using brackets as shown below.

JArray

Sometimes the JSON data contains multiple objects that we need to process. Lets say that we would have some JSON data containing multiple person objects that we processed in the example above. Below is an example of such JSON data, which we save to a variable.

We can then parse the JSON data into a JObject using a Frends.JSON.ConvertJsonStringToJToken Task in the same way that we did above with the single piece of JSON data. The JObject.Parse method will not work, since the data is not a single piece of JSON data, but an array containing multiple JSON data entries. Hence, we need to use the JArray.Parse method to create a JArray from the data.

We can then access each of the values in the JArray in the same way as with any other array, using indexes. You can think that JArray is basically an array of objects.

Since a JArray is basically an array of objects, we can also iterate the array using a Foreach Element. Below is an example of a Foreach Element's parameters, in which we iterate through the JArray that we created before and we save each of the values into a variable named "singleJsonData" in each iteration.

We can then access the data of each JObject in the JArray in the same way as we accessed the data in the above JObject example, using dot notation.

From object to JSON

After creating a JObject or JArray, and processing the data, you might want to transform the object back to a JSON string so you are able to use the data in some other Element, such as in a Frends.Web.RestRequest Task or a Frends.Csv.Create Task. This can be easily achieved by calling the JToken.ToString method. Since both JObject and JArray inherit the JToken class, the ToString method is available in both classes. The method overrides the Object class' ToString method so that the method call will return a JSON string representation of the data stored in the object instead of information about the object itself. Below is an example of transforming the above JArray into a JSON string and the data which is produced by the method call.

Did this answer your question?