Reading AMPscript Variables in SSJS

%%[
  SET @subscriberKey = _subscriberkey
  SET @emailAddr = emailaddr
  SET @firstName = FirstName
]%%

<script runat="server">
var subKey = Platform.Variable.GetValue("@subscriberKey");
var email = Platform.Variable.GetValue("@emailAddr");
var firstName = Platform.Variable.GetValue("@firstName");

// Now use these in SSJS logic
var score = Platform.Function.Lookup("LeadScores", "score", "email", email);
Platform.Variable.SetValue("@leadScore", score);
</script>

%%[ /* Render the SSJS-computed value in AMPscript */ ]%%
Lead score: %%=v(@leadScore)=%%

Passing SSJS Results to AMPscript Rendering

// SSJS computes complex logic
var tier = "standard";
if (parseInt(score, 10) > 80) tier = "premium";
if (parseInt(score, 10) > 95) tier = "vip";

Platform.Variable.SetValue("@tier", tier);
Platform.Variable.SetValue("@discountCode", discountCodes[tier]);
%%[ IF @tier == "vip" THEN ]%%
  <div class="vip-banner">Welcome, VIP!</div>
  <p>Your exclusive code: %%=v(@discountCode)=%%</p>
%%[ ELSEIF @tier == "premium" THEN ]%%
  <p>Premium member discount: %%=v(@discountCode)=%%</p>
%%[ ELSE ]%%
  <p>Standard member</p>
%%[ ENDIF ]%%

Safe URL Encoding via AMPscript

AMPscript’s URLEncode has more encoding options than SSJS:

Variable.SetValue("@rawValue", userInput);
TreatAsContent("%%[SET @encoded = URLEncode(@rawValue, 1, 1)]%%");
var encoded = Variable.GetValue("@encoded");

Using TreatAsContent Safely

// SAFE pattern
Variable.SetValue("@name", userName);
Variable.SetValue("@code", promoCode);
TreatAsContent("Hello, %%=v(@name)=%%. Your code is %%=v(@code)=%%.");

// DANGEROUS — never do this:
// TreatAsContent(userInput); // AMPscript injection!

Reading Subscriber Attributes in SSJS

%%[
  SET @city = AttributeValue("City")
  SET @language = AttributeValue("PreferredLanguage")
]%%

<script runat="server">
var city = Platform.Variable.GetValue("@city");
var language = Platform.Variable.GetValue("@language") || "en";

// Or use the global Attribute object directly
var city2 = Attribute.Value("City");
</script>

JSON Data Bridge

Pass complex data from AMPscript to SSJS via JSON strings:

%%[
  SET @productJson = LookupRows("Products", "Category", "featured")
]%%

<script runat="server">
// Better: use SSJS to retrieve directly
var products = Platform.Function.LookupRows("Products", "Category", "featured");
// products is already an array in SSJS
</script>

For data computed in SSJS and consumed in AMPscript, use simple string variables since AMPscript doesn’t parse JSON natively:

// SSJS
Platform.Variable.SetValue("@productCount", products.length);
Platform.Variable.SetValue("@topProduct", products[0] ? products[0].Name : "");
%%[ /* AMPscript */ ]%%
We have %%=v(@productCount)=%% featured products.
Top pick: %%=v(@topProduct)=%%

See Also