Use calculated fields to export arrays as strings use-calculated-fields-to-export-arrays-as-strings
Learn how to export arrays through calculated fields from Real-Time CDP to cloud storage destinations as strings. Read this document to understand the use cases enabled by this functionality.
Get extensive information about calculated fields - what these are and why they matter. Read the pages linked below for an introduction to calculated fields in Data Prep and more information about all the available functions:
Arrays and other object types in Platform arrays-strings-other-objects
In Experience Platform, you can use XDM schemas to manage different field types. Before support for array exports was added, you were able to export simple key-value pair type fields such as strings out of Experience Platform to your desired destinations. An example of such a field that was supported for export previously is personalEmail.address
:johndoe@acme.org
.
Other field types in Experience Platform include array fields. Read more about managing array fields in the Experience Platform UI. In addition to the previously supported field types, you can now export array objects such as the example below, concatenated into a string by using the array_to_string
function.
organizations = [{
id: 123,
orgName: "Acme Inc",
founded: 1990,
latestInteraction: "2024-02-16"
}, {
id: 456,
orgName: "Superstar Inc",
founded: 2004,
latestInteraction: "2023-08-25"
}, {
id: 789,
orgName: 'Energy Corp',
founded: 2021,
latestInteraction: "2024-09-08"
}]
See further below extensive examples of how you can use various functions to access elements of arrays, transform and filter arrays, join array elements into a string, and more.
Known limitations known-limitations
Note the following known limitations that currently apply to this functionality:
- Export to JSON or Parquet files with hierarchical schemas is not supported at this time. You can export arrays to CSV, JSON, and Parquet files as strings only, by using the
array_to_string
function.
Prerequisites prerequisites
Connect to a desired cloud storage destination, progress through the activation steps for cloud storage destinations and get to the mapping step.
How to export calculated fields how-to-export-calculated-fields
In the mapping step of the activation workflow for cloud storage destinations, select Add calculated field.
This opens a modal window where you can select functions and fields to export attributes out of Experience Platform.
For example, use the array_to_string
function on the organizations
field as shown below to export the organizations array as a string in a CSV file. View more information about this and other examples further below.
Select Save to keep the calculated field and return to the mapping step.
Back in the mapping step of the workflow, fill in the Target field with a value of the column header you want for this field in the exported files.
When ready, select Next to proceed to the next step of the activation workflow.
Sample supported functions to export arrays supported-functions
All the documented Data Prep functions are supported when activating data to file-based destinations.
The functions below, specific to handling exports of arrays, are documented along with examples.
array_to_string
flattenArray
filterArray
transformArray
coalesce
size_of
iif
index-based array access
add_to_array
to_array
first
last
Examples of functions used to export arrays examples
See examples and further information in the sections below for some of the functions listed above. For the rest of the listed functions, refer to the general functions documentation in the Data Prep section.
array_to_string
function to export arrays array-to-string-function-export-arrays
Use the array_to_string
function to concatenate the elements of an array into a string, using a desired separator, such as _
or |
.
For example, you can combine the following XDM fields below as shown in the mapping screenshot by using an array_to_string('_',organizations)
syntax:
organizations
arrayperson.name.firstName
stringperson.name.lastName
stringpersonalEmail.address
string
In this case, your output file looks like below. Notice how the elements of the array are concatenated into a single string using the _
character.
First_Name,Last_Name,Personal_Email,Organization
John,Doe,johndoe@acme.org, "{'id':123,'orgName':'Acme Inc','founded':1990,'latestInteraction':1708041600000}_{'id':456,'orgName':'Superstar Inc','founded':2004,'latestInteraction':1692921600000}_{'id':789,'orgName':'Energy Corp','founded':2021,'latestInteraction':1725753600000}"
filterArray
function to export filtered arrays
Use the filterArray
function to filter the elements of an exported array. You can combine this function with the array_to_string
function described further above.
Continuing with the organizations
array object from above, you can write a function like array_to_string('_', filterArray(organizations, org -> org.founded > 2021))
, returning the organizations with a value for founded
in the year 2021 or more recent.
In this case, your output file looks like below. Notice how the two elements of the array that meet the criterion are concatenated into a single string using the _
character.
John,Doe,johndoe@acme.org, "{'id':123,'orgName':'Acme Inc','founded':1990,'latestInteraction':1708041600000}_{'id':789,'orgName':'Energy Corp','founded':2021,'latestInteraction':1725753600000}"
transformArray
function to export transformed arrays
Use the transformArray
function to transform the elements of an exported array. You can combine this function with the array_to_string
function described further above.
Continuing with the organizations
array object from above, you can write a function like array_to_string('_', transformArray(organizations, org -> ucase(org.orgName)))
, returning the names of the organizations converted to all uppercase.
In this case, your output file looks like below. Notice how the three elements of the array are transformed and concatenated into a single string using the _
character.
John,Doe,johndoe@acme.org,ACME INC_SUPERSTAR INC_ENERGY CORP
iif
function to export arrays iif-function-export-arrays
Use the iif
function to export elements of an array under certain conditions. For example, continuing with the organizations
array object from above, you can write a simple conditional function like iif(organizations[0].equals("Marketing"), "isMarketing", "isNotMarketing")
.
In this case, your output file looks like below. In this case, the first element of the array is Marketing, so the person is a member of the marketing department.
`First_Name,Last_Name, Personal_Email, Is_Member_Of_Marketing_Dept
John,Doe, johndoe@acme.org, "isMarketing"
add_to_array
function to export arrays add-to-array-function-export-arrays
Use the add_to_array
function to add elements to an exported array. You can combine this function with the array_to_string
function described further above.
Continuing with the organizations
array object from above, you can write a function like source: array_to_string('_', add_to_array(organizations,"2023"))
, returning the organizations that a person is a member of in the year 2023.
In this case, your output file looks like below. Notice how the three elements of the array are concatenated into a single string using the _
character and 2023 is also appended at the end of the string.
`First_Name,Last_Name,Personal_Email,Organization_Member_2023
John,Doe, johndoe@acme.org,"Marketing_Sales_Finance_2023"
flattenArray
function to export flattened arrays
Use the flattenArray
function to flatten an exported multidimensional array. You can combine this function with the array_to_string
function described further above.
Continuing with the organizations
array object from above, you can write a function like array_to_string('_', flattenArray(organizations))
. Note that the array_to_string
function flattens the input array by default into a string.
The resulting output is the same as for the array_to_string
function described further above.
coalesce
function to export arrays coalesce-function-export-arrays
Use the coalesce
function to access and export the first non-null element of an array into a string.
For example, you can combine the following XDM fields below as shown in the mapping screenshot by using a coalesce(subscriptions.hasPromotion)
syntax to return the first true
of false
value in the array:
"subscriptions.hasPromotion": [null, true, null, false, true]
arrayperson.name.firstName
stringperson.name.lastName
stringpersonalEmail.address
string
In this case, your output file looks like below. Notice how the first non-null true
value in the array is exported in the file.
First_Name,Last_Name,hasPromotion
John,Doe,true
size_of
function to export arrays sizeof-function-export-arrays
Use the size_of
function to indicate how many elements exist in an array. For example, if you have a purchaseTime
array object with multiple timestamps, you can use the size_of
function to indicate how many separate purchases were made by a person.
For example, you can combine the following XDM fields below as shown in the mapping screenshot.
"purchaseTime": ["1538097126","1569633126,"1601255526","1632791526","1664327526"]
array indicating five separate purchase times by the customerpersonalEmail.address
string
In this case, your output file looks like below. Notice how the second column indicates the number of elements in the array, corresponding with the number of separate purchases made by the customer.
`Personal_Email,Times_Purchased
johndoe@acme.org,"5"
Index-based array access index-based-array-access
You can access an index of an array to export a single item from the array. For example, similar to the example above for the size_of
function, if you are looking to access and export only the first time that a customer has purchased a certain product, you can use purchaseTime[0]
to export the first element of the timestamp, purchaseTime[1]
to export the second element of the timestamp, purchaseTime[2]
to export the third element of the timestamp, and so on.
In this case, your output file looks like below, exporting the first time that the customer has made a purchase:
`Personal_Email,First_Purchase
johndoe@acme.org,"1538097126"
first
and last
functions to export arrays first-and-last-functions-export-arrays
Use the first
and last
functions to export the first or last element in an array. For example, continuing with the purchaseTime
array object with multiple timestamps from the previous examples, you can use these to functions to export the first or the last purchase time made by a person.
In this case, your output file looks like below, exporting the first and the last time that the customer has made a purchase:
`Personal_Email,First_Purchase, Last_Purchase
johndoe@acme.org,"1538097126","1664327526"