Skip to content

Latest commit

 

History

History
138 lines (99 loc) · 3.74 KB

File metadata and controls

138 lines (99 loc) · 3.74 KB
description
Transforms a list into an object with extracted key-value pairs.

Convert to Object

Overview

This transform allows you to convert a list of objects into a single object, creating each object's property using user-defined key-value pairs extracted from the list, and simplifying the handling of complicated list structures.


Parameters

ParameterDescriptionRequiredRequired
List to TransformThe list of objects you want to transform into a single object. Each item in the list should be a object with key-value pairs.truetrue
Field to use as KeyThe field name from the items in your list that you want to use as keys in your new object.truetrue
Field to use as ValueThe field name from the items in your list that you want to use as values in your new object.truetrue

{% hint style="info" %} For nested field names, separate them by dots (e.g., details.age). {% endhint %}

Usage

The Convert to Object transform provides a powerful way to reshape your data to meet specific requirements. The ability to define keys and values, including the ability to access nested data, makes it versatile for a range of scenarios. Let's go through a couple of use-case examples to demonstrate how this transform can be applied to your data.

Example 1: Simple Key-Value Conversion

Let's assume we have this list of objects called mylist:

mylist: [
  {
    name: "John",
    age: 30,
    hobbies: ["golf", "reading"],
  },
  {
    name: "Mary",
    age: 35,
    hobbies: ["cooking", "music"],
  },
]

Action Parameters:

We want to create a new object where the keys are the name field and the values are the age field. We would use the following parameters:

key_field: name
value_field: age

Jinja2 Equivalent:

{% raw %}
{% set new_object = {} %}
{% for item in mylist %}
  {% set _ = new_object.update({item['name']: item['age']}) %}
{% endfor %}
{% endraw %}

Example 2: Nested Key-Value Conversion

Now, let's consider a list of objects where some fields are nested:

mylist: [
  {
    name: "John",
    details: {
      age: 30,
      occupation: "Engineer"
    },
    hobbies: ["golf", "reading"],
  },
  {
    name: "Mary",
    details: {
      age: 35,
      occupation: "Doctor"
    },
    hobbies: ["cooking", "music"],
  },
]

Action Parameters:

In this case, the age field is nested under the details object. So, to create an object where the keys are the name field and the values are the age field nested under details, we would use the following parameters:

key_field: name
value_field: details.age

Jinja2 Equivalent:

{% raw %}
{% set new_object = {} %}
{% for item in mylist %}
  {% set _ = new_object.update({item['name']: item['details']['age']}) %}
{% endfor %}
{% endraw %}

Results Output

In both of these examples, your new output object would look as follows:

results: {
  "John": 30,
  "Mary": 35
}

You've learned how to simplify complex lists using the Convert to Object transform. Remember, the key to using this transform effectively is understanding your data's structure and identifying the appropriate key-value pairs that will make your object meaningful.

{% hint style="info" %} This transform is especially useful when dealing with data structures like ConnectWise PSA Custom Fields or Communication Items. {% endhint %}