Set Field Value

Modify a specific field's value within your list objects.

Use Case

You have a list of objects where certain field values need adjusting. You need a solution that not only allows straightforward field value modifications, but also supports conditional changes and nested element modifications.

Overview

The Set Field Value action enables the customization of a field's value within a list of objects. Whether it's for conditional value adjustment based on specific criteria or working with nested elements, this action offers the flexibility you need.


Parameters

ParameterDescriptionRequired

List to Transform

The list of objects you want to modify the contents of.

Field to Update

Specify the field to be added or modified in each object.

New Value

Specify the value for the new or modified field.

New Value Mode

How you want to provide the field's new value. copy_from_field or set_value (described below)

Parent Fields

If the field should be nested within another list, specify the parent list's name here.

Condition Field

If you want to update the field conditionally, specify the field to base the condition on.

Condition Value

If a Condition Field is specified, provide the value that should be matched to trigger the update.

Usage

Let's break this down into specific use-case examples, to show how each of these methods can be used within the Set Field Value Transform.

Input List

Assume that we have a list of objects called my_list that looks like this:

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

Update Methods

Using the New Value Mode you can define how you want to provide the data for the outputting field. You can do one of two things:

  • Copy From Field: allows you to copy the value from an existing field

  • Set Value: provide the literal value you'd like the field to be set to.

Here are some examples of how we can use this action to update or add to our list:

Example 1: Create and set new field values

Add a new field adult with value true to each object in the list.

Action Parameters:

field_actions:
 field: adult
 new_value: true
 new_value_mode: set_value

Jinja2 Equivalent:

{% set _ = item.update({'adult': true}) %}

Example 2: Copy value from existing field to new field

Copy the age field to a new field years in each object.

Action Parameters:

field_actions:
 field: years
 new_value: age
 new_value_mode: copy_from_field

Jinja2 Equivalent:

{% set _ = item.update({'years': item['age']}) %}

Example 3: Update fields conditionally based on other fields

Update the name field to Senior for any object where age is 35.

Action Parameters:

field_actions:
 field: name
 new_value: "Senior"
 new_value_mode: set_value
 condition_field: age
 condition_value: 35

Jinja2 Equivalent:

{% if item['age'] == 35 %}
  {% set _ = item.update({'name': 'Senior'}) %}
{% endif %}

Results Output

After all these examples are performed in the transformation, your newly updated list would reflect these changes in their outputted results as such:

results: [
  {
    name: "John",
    age: 30,
    hobbies: ["golf", "reading"],
    adult: true,
    years: 30
  },
  {
    name: "Senior",
    age: 35,
    hobbies: ["cooking", "music"],
    adult: true,
    years: 35
  },
]

With the knowledge of the Set Field Value action, you're now equipped to make modifications to the field values in your lists of objects. Remember, the aim is not just to change values but to improve the readability and utility of your data.

Last updated