Working with complex nested data objects can be difficult, especially when accessing a specific property or subset of data in a complex data structure. This post will look at complex data objects, what they are, and an easier way of managing those complex data objects by using complex types.
What is a data object?
A data object is a structured collection of properties, additional objects or a list of objects. A complex data object is any structure that combines various fields and sometimes a list of fields or other objects. There are many ways to store and interact with such a data object.
An example of a data object:
A collection of people where each person has a collection of cars. A person has a name, surname and identification number, and each car has its attributes. In a traditional relational data model, the cars will be stored in a car table with a foreign key pointing to the person’s table, thus attaching that specific car to the person.
When passing around data or working with it in an API context, you may be more interested in using an entire data structure, such as the person with all their cars. Here it will be helpful to have an object containing many values or properties relating to that data object. JSON objects simplify this. JSON is a great way to transfer complex data objects as its key-value architecture makes it flexible and understandable. The only difficulty with JSON is that it can become confusing for a person to manipulate once the structure becomes complex.
Linx uses types to manage the complexity of implementing data objects. In Linx, you can create a Custom Type to represent the data object, allowing easier manipulation and access.
Building data objects in Linx – Types
Linx uses Types to handle data and values. The basic types are integers, strings and booleans, (amongst others). These simple types are only able to handle a single value. In addition to these types, Linx allows you to create or import your own complex types. These complex types are data objects with a collection of properties, much like JSON objects. You can easily create an object for a car containing the relevant properties, then create another object for a person with its properties and a list of cars. The result will look similar to the example data structure in the previous section.
Once the type is created, you can use it in your functions as you would use any type. Here are some things you can do with custom types
- Use it as a variable and assign values to it
- Create a list of the type (for example, you have multiple people you need to store)
- Pass the type around as the parameters or result of functions
- Use the type as a Request or Response body of a REST Host
- Use the type as the response from a REST call, allowing for easier access to data or specific properties in the response
When using custom types in a REST solution, these types translate to JSON objects, allowing you to format a response body with the type builder easily. Additionally, you can create a type by importing JSON or XML. A detailed tutorial can be found here.
Custom types
Custom types are highly flexible and can be used in several ways. A common use case is the ability to handle data and make it more manageable.
An example;
- A custom type for a person with a list of cars has been created.
- A function will retrieve that list of people.
- Then the cars for each person need to be retrieved.
- This data is in a SQL database in two separate tables.
The screenshot above outlines a function where the custom type is used in a list. The function then
- retrieves all persons from the database,
- loops through those customers, and
- adds in each car that belongs to a person.
- Finally, the function will return the list of persons with their cars as illustrated in the Debug Values tab (on the right side of the Linx Designer)
The function can then be `re-used later to return the list as the response of a REST API call or for further processing. The returned type will have the values assigned, meaning you can reference a specific value, loop through each person, loop through the list of cars for each person and so forth. This way, you can control each data element and manipulate or consume it as required.
Low-code advantage
Custom types are often used to simplify data handling and manipulation. You can reference any specific property or value in the custom type with easily digestible and manageable data objects. By doing so, you do not need to use regex or other complex processes to extract and transform a particular property in a data structure.