AMPscript LookUp and Output JSON

Charlie Fay
3 min readMar 22, 2024

--

Photo by Florian Krumm on Unsplash

Marketers and developers often find themselves juggling various scripting languages to achieve their desired outcomes. AMPscript, a powerful scripting language primarily used for email personalisation within the Salesforce Marketing Cloud platform, is undoubtedly a staple in this landscape. However, there are instances where the limitations of AMPscript prompt the need for alternative solutions, such as working with Server-Side JavaScript and arrays to enhance functionality. In this blog post, we’ll explore how combining AMPscript with JavaScript arrays can elevate your dynamic content strategies.

The Power of AMPscript

AMPscript has long been celebrated for its ability to personalise content within emails, landing pages, and other digital touchpoints in SFMC. Its server-side execution enables marketers to tailor messaging based on subscriber attributes, purchase history, or any other data stored within their Salesforce Marketing Cloud instance. However, there are scenarios where the need arises to pass server-side information to the client-side for rendering dynamic content or facilitating interactivity.

Enter JavaScript Arrays

JavaScript arrays provide a flexible and robust means of organising and manipulating data within client-side scripts. Leveraging JavaScript alongside AMPscript opens up many possibilities for creating dynamic, personalised experiences. One common requirement is the output of JSON-formatted data, which, by default, isn’t directly supported by AMPscript. This is where the marriage of AMPscript and JavaScript arrays becomes invaluable.

Constructing JSON-like Strings with AMPscript

While AMPscript lacks native JSON support, it offers a robust set of string manipulation functions that allow for the construction of JSON-like strings. By leveraging these functions, it’s possible to generate JSON structures dynamically within AMPscript. This capability enables seamless communication of server-side data to client-side JavaScript.

Practical Example: Generating JSON with AMPscript and JavaScript

Let’s consider a practical example where we want to pass subscriber information from a Salesforce Marketing Cloud CloudPage to a client-side JavaScript function for dynamic rendering.

We’ll start by retrieving subscriber data using AMPscript and then construct a JSON string for consumption by JavaScript.

%%[

SET @id = '001173250'

SET @pgRows = LookupOrderedRows('DE_Lookup', 0, 'id asc', 'id', @id)
SET @pgRowsReturned = RowCount(@pgRows)

SET @pgFields = 'rank|id|name|description|product|url' /* list field names here */
SET @pgFieldsRows = BuildRowsetFromString(@pgFields, '|')
SET @pgFieldsRowsReturned = RowCount(@pgFieldsRows)


IF @pgRowsReturned > 0 THEN

SET @array = ''

/* Cycle each returned data row */
FOR @i=1 TO @pgRowsReturned DO

/* Open object row */
SET @array = Concat(@array, '{')

/* Add key-value pairs to opened object */
FOR @j=1 TO @pgFieldsRowsReturned DO
SET @dynVarName = Field(Row(@pgFieldsRows,@j), 1) /* use ordinal value in FIELD() function */
SET @dynVarValue = Field(Row(@pgRows,@i),TreatAsContent(@dynVarName))
SET @array = Concat(@array, '"', @dynVarName, '":"', @dynVarValue, '"'))
SET @array = Concat(@array, IIF(@j==@pgFieldsRowsReturned, '', ',')) /* Add comma if not the last field */
NEXT @j

/* Close object row */
SET @array = Concat(@array, IIF(@i==@pgRowsReturned, '}', '},')) /* Add comma if not the last row */

NEXT @i

ELSE
/* handle error */
ENDIF

SET @jsonArray = Concat('[', @array, ']')

]%%

%%=v(@jsonArray)=%% <!-- Write output to your page -->
<script runat="client" language="javascript">
var subscriberData = JSON.parse('%%=v(@jsonArray)=%%');
console.log(subscriberData)
</script>

In this example, we use AMPscript to retrieve subscriber attributes. We then construct a JSON-like string using concatenation. Finally, we pass this string to a JavaScript function for further processing.

Conclusion

While AMPscript serves as a robust tool for server-side personalisation, its integration with client-side JavaScript arrays opens up new avenues for dynamic content delivery and interactivity. By leveraging AMPscript’s string manipulation capabilities alongside JavaScript arrays, we can create seamless, personalised experiences that resonate with our audience.

--

--

Charlie Fay
Charlie Fay

Written by Charlie Fay

I write about Salesforce Marketing Cloud, Marketing Automation, Development and Programmatic Languages. I enjoy solving complex problems with simplicity.

No responses yet