BLOCKS Reference

Basic

Convert array of objects

Overview

With this BLOCK, you can convert the data within each element of an array. You won’t be asked for the pre-conversion data types for each element in the array, but each element in the post-conversion array will become an Object. During the conversion, the BLOCK will use a set of conversion rules you define on each element in the array. The number of elements in the pre and post-conversion arrays will stay the same.

Overview of Convert Array of Objects BLOCK

The following is an example of a conversion using this BLOCK (JSON format array of objects):

  • Before converting:
    [
      {
        "first_name": "Jane",
        "last_name": "Austen",
        "book1": "Pride and Prejudice",
        "book2": "Sense and Sensibility",
        "book3": "Mansfield Park"
      },
      {
        "first_name": "Mark",
        "last_name": "Twain",
        "book1": "Adventures of Huckleberry Finn",
        "book2": "The Adventures of Tom Sawyer",
        "book3": "A Connecticut Yankee in King Arthur's Court"
      },
      {
        "first_name": "William",
        "last_name": "Shakespeare",
        "book1": "Hamlet",
        "book2": "Macbeth",
        "book3": "Romeo and Juliet"
      }
    ]
    
  • After conversion:
    [
      {
        "name": "Jane Austen",
        "masterpieces": [
          "Pride and Prejudice",
          "Sense and Sensibility",
          "Mansfield Park"
        ],
        "language": "English"
      },
      {
        "name": "Mark Twain",
        "masterpieces": [
          "Adventures of Huckleberry Finn",
          "The Adventures of Tom Sawyer",
          "A Connecticut Yankee in King Arthur's Court"
        ],
        "language": "English"
      },
      {
        "name": "William Shakespeare",
        "masterpiece": [
          "Hamlet",
          "Macbeth",
          "Romeo and Juliet"
        ],
        "language": "English"
      }
    ]
    

The following shows specifically how the first element in the array was converted:

Diagram of conversion with the Convert array of objects BLOCK

As you can see, you can use this BLOCK to do things like combine values or add new values. While not shown in the above example, you can also delete, or omit, values from the pre-conversion array.

To perform this kind of conversion, you must set conversion rules for the post-conversion object.

For the above example, the conversion rules are as follows:

Example conversion rules for the Convert array of objects BLOCK

You must set the key, value type, and value, for each key:value pair in the post-conversion object.

You can select from the following eight types for the value type:

Type Explanation
String

Designates the value as a string. You can enter a key from the pre-conversion object within${ and } tags to reference the value of the key. The example above uses ${first_name} and ${last_name} to reference the values of the keys first_name and last_name in the pre-conversion object. You can also use % format characters with this type.

String (old)

Designates the value as a string. This is mostly the same as the String type explained above, but you cannot use % format characters.

Integer

Designates the value as an integer.

Float

Designates the value as a float.

Boolean

Designates the value as a Boolean true or false.

Variable

Designates the value as a variable.

  • You can designate a variable and the BLOCK will reference the contents of the variable for the conversion.
  • If the element in the pre-conversion array is an object, you can designate its keys. If the name of an object’s key is the same as a variable name, the object’s key will take priority.

    In the example above, book1, book2, and book3 reference the keys book1, book2, and book3 in the pre-conversion object.

  • You can use the special variable _row to reference each element in the pre-conversion array.

    For example, if the original array was as follows:

    [
      [10, 20, 30, "foo"],
      [11, 21, 31, "bar"],
      [12, 22, 33, "baz"]
    ]
    

    If you used the special variable _row.0, it would reference the first value in each element of the array (10, 11, 12)

  • You can use expanded variables.
Object

Designates the value as an object.

Array

Designates the value as an array.

Properties

Property Explanation
BLOCK name Configure the name displayed on this BLOCK.
Source variable

Designate the variable that contains array data to be converted.

Results variable

Designate the variable that will contain the newly-converted array of objects.

Conversion rules

Define the format of the post-conversion object.

BLOCK memos Make notes about this BLOCK.