Open Navigation

Object Helper

Interact with objects
On This Page

Overview

The Object Helper allows you to interact with objects


Basic Operations List


  • Add key/ value pairs

  • Add value by key

  • Contains

  • Enforce object structure

  • Equals

  • Get value by key

  • Iterative transform

  • JSON Parse

  • JSON Stringify

  • Merge two objects


Example 1: Create and manipulate object


The following example creates an object and applies various transformations to illustrate how you can use the connector.

Create a connector with a Manual Trigger. Add an Object Helpers step set to Add value/ key pairs operation.

Create a new object by adding a property to the Source, for instance with property name name and any value you wish. Add a new item to the List parameter, as illustrated below (note we have switched to the Debug tab to view the output):

object-helpers-workflow-1

The result property in the output holds the newly created object.

Add a step with the operation set to Add value by key. We will use this to add another property to our object. Set the Object property to the result of the previous step, in Key enter the name for a new property to add to the object, and in Value specify its value:

object-helpers-workflow-2

The result property in the output will show the updated object.

We now use the Enforce object structure operation to enforce the structure of our object. Set the Object to the output of the previous step, change the field type of the Structure parameter to object, and set a couple of fields to check for:

object-helpers-workflow-3

We then use the Merge two objects operation to assign a value to the property created by the previous step. Change the type of Object two to object, add property with property name set to user_order, for instance. Set the new value for this property.

object-helpers-workflow-4

Finally, we use the Iterative transform operation to enforce the style of property keys and values. Set the Key transform parameter to "camelCase", and Value transform to Start Case as follows:

object-helpers-workflow-5

The result is an object in which all keys are camel cased and all string values are in start case.

Example 2: Take existing object and parse fields

In this example we take the output from another connector step and manipulate the object ready for importing into another connector in a Tray workflow.

Imagine a connector step which outputs the details of a person as:

"person": {
"emailAddress": "Tray Connectors <tray-connectors@example.com>",
"website": "www.example.com",
"address": "1 sample street",
"city": "Sample city",
"zip": "12345"
}

But we have another connector step which requires input in the following format:

"person": {
"first_name": string,
"last_name": string,
"email_address": string,
"website": string,
"city": string,
"zip_code": string
}

We will use the Object Helpers to convert the first object into the following:

"person": {
"first_name": "Tray",
"last_name": "Connectors",
"email_address": "tray-connectors@example.com",
"website": "https://www.example.com",
"city": "Sample city",
"zip_code": "12345"
}

The first step is to create a workflow. In this instance as we are creating an example, we will use the Script connector, set to the Execute Script operation, to generate a JSON object illustrative of connector output. In Script we enter

exports.step = function() {
return {
"emailAddress": "Tray Connectors <tray-connectors@example.com>",
"website": "www.example.com",
"address": "1 sample street",
"city": "Sample city",
"zip": "12345"
}
};

object-helpers-workflow-2-1

Then we use the Iterative transform operation of the Object helpers to snake case all the property names, by setting Source object to the result of the previous step and Key transform to snake_case:

object-helpers-workflow-2-2

Next we use the Extract all by Regular Expression operation of the Text helpers to extract the first name, last name and email address from the email_address field by setting the Text to apply regex on to $.steps.object-helpers-1.result["email_address"], and the Regex field to /(.+?) (.+) <?(.+@.+[^>])>?$/:

object-helpers-workflow-2-3

Finally, we create a new object as our output with all the properties we wish to include by inserting an Object helpers step set to the Add key/value pairs operation, with the Source left as is (empty object). Then click Add item under the List parameter for each key/value pair to create in our output object (we have 5 properties).

There will be a new item for each of the following key/value pairs:

  • first_name: $.steps.text-helpers-1.result[0][1],
  • last_name: $.steps.text-helpers-1.result[0][2],
  • email_address: $.steps.text-helpers-1.result[0][3],
  • website:: $.steps.script-1.result.website,
  • city: $.steps.script-1.result.city,
  • zip_code: $.steps.script-1.result.zip:

object-helpers-workflow-2-4

The output of this operation will be our final object.


Full operations reference


Add key/ value pairs

Adds key/ value pairs to a new or existing object.

Source: (required) Source object to add items to. Can be an empty object (see example).

List: (required) Array of items to add to the Source object.

Add value by key

Adds a value to an object by its key.

Object: (required) Object to add value to.

Key: (required) Key on which to set new value.

Value: (required) Value to set Key to in Object.

Contains

Returns true if an object contains a key at the top level, false otherwise.

Object: (required) Object that the operation should be performed on.

Key: (required) Property that the object should have.

Enforce object structure

Defines a JSON structure which the output object must adhere to. For instance, given the Object {"name": "Tray"} and the Structure {"surname": "anyValue"} the result would be {"name": "Tray", "surname": null} as the operation forces Object to have the "surname" property even with a null value.

Object: (required) Object that should adhere to a structure.

Structure: (required) JSON structure that the output object must adhere to.

The property type for Object and Structure can either be Object or String (with valid JSON).

Equals

Allows you to compare two objects.

Object: (required) First object to compare.

Object two: (required) Second object to compare.

Get value by key

Returns the value of an object property.

Object: (required) Object that the operation should be performed on.

Key: (required) Object property the value should be returned from. Can also be a path.

Iterative transform

Allows you to transform keys and values in an object. Specify one or all of Key transform and Value transform.

Source object: (required) Object to iterate through.

Key transform: Specify the action to perform on keys.

Value transform: Specify the action to perform on values.

JSON parse

Creates an object by parsing a valid JSON string.

Text: (required) JSON string.

JSON stringify

Transforms a JSON object into a string.

JSON: (required) JSON object.

Merge two objects

Merges two objects. If a property is specified in both objects, the one specified in the second will be used. For instance, given Object {"name": "Foo", "surname": "io"} and Object two {"name": "Tray"}, the result would be {"name": "Tray", "surname": "io"}.

Object: (required) First object.

Object two: (required) Second object.

The property type for Object and Object two can either be Object or String (with valid JSON).

Was this article helpful?
Yes
No
On This Page