0

I have a basic javascript for retrieving values. Like this:

    <script runat="server">
    Platform.Load("core", "1.1");
    
    var list = ["value1","value2"];
    for (var i =0; i < list.length; i++) {
        Variable.SetValue("@element", list[i]);
    </script>
    
    %%[
        /* Put your AMPscript here */
        Output(Concat(@element, '<br>'));
    ]%%
    
    <script runat="server"> 
    }
    
    </script>

Now I have a data extension with a JSON in a attribute field (["value1","value2"]). How can I adjust my script so I got the JSON data from that data extention? And is there a way to act on the data? So show some text with value1 and another text with value2.

3
  • 1
    It’s not JSON, it’s an array. What exactly do you want to do - have a variable for each of the array elements?
    – zuzannamj
    Commented Nov 8, 2022 at 10:15
  • So, in our data we will receive the array like tis: ["value1","value2"] We need everything in the array but based on the different values we have to show some other text. So for value1 some text and for value2 another etc.
    – Martijn e
    Commented Nov 8, 2022 at 11:08
  • That’s what I did in the example posted in my answer below
    – zuzannamj
    Commented Nov 8, 2022 at 11:12

1 Answer 1

3

You can retrieve the array from the DE either using an AttributeValue function if the data sits in your sendable DE, or using a Lookup if it's in a related DE. You did not provide any details about your data structure, so I am assuming it's sitting in your sendabe DE like this:

enter image description here

Also I am assuming you want to display this inside an email, so I'd use AMPscript instead of SSJS to pull and parse the data using the BuildRowsetFromString function:

%%[
set @data = AttributeValue("data") /* get data from a sendable DE */

/* build a simple string from array */
set @string = replace(@data, "[", "") 
set @string = replace(@string, "]", "")
set @string = replace(@string, '"', '')

/* build a rowset */
set @rows = BuildRowsetFromString(@string,",")
set @rowCount = rowCount(@rows)

if @rowCount > 0 then

  for @i = 1 to @rowCount do

    set @row = row(@rows, @i) /* get row based on counter */
    set @value = field(@row,1)

    /* conditionally display text based on array data */
    if @value == "value1" then ]%%
    <br>some text for value 1<br>
    %%[ elseif @value == "value2" then ]%%
    <br>some text for value 2<br>
    %%[ elseif @value == "value3" then ]%%
    <br>some text for value 3<br>
    %%[ else ]%%
    <br>fallback text for other values<br>
    %%[ endif

  next @i 
  endif ]%%

I've added some conditions above (%%[ if @value == "value1" then ]%% <br>some text for value 1<br> etc.) to conditionally display text based on the values in the array, but without understanding what your use case is exactly it's hard to point to the best solution.

3
  • Yes this is exactly what I mean. Thanks,
    – Martijn e
    Commented Nov 8, 2022 at 12:58
  • 1
    This is a great answer and approach to handling Arrays! I would just adjust the replaces to: replace(@data, '["','') and replace(@string, '"]','') and then replace(@string,'","',',') to help remove chance of accidently removing a double quote or bracket that is in the value of the array. Also as a note, this is also completely possible in SSJS but if you are going to be doing AMPscript in the email as well, it is then highly recommended you remain in a single scripting language for performance reasons as zuzannamj stated. Commented Nov 10, 2022 at 14:42
  • Thanks for the tip Greg!
    – zuzannamj
    Commented Nov 10, 2022 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .