0

we have a simple JSON structure. This structure has no names like this:

["value1","value2"]

Is there a way to parse/use this in Marketing Cloud? We don't have to make decisions. Everything in the JSON we will use.

2
  • Yes. This is an array of strings, so parse it into a List<String>.
    – Phil W
    Nov 8, 2022 at 7:05
  • I am working in the Marketing Cloud. So if I understand you correctly we can do this with javascropt/SSJS in our emails with List<String>?
    – Martijn e
    Nov 8, 2022 at 7:30

2 Answers 2

1

Adding an alternative answer: assuming you want to display this inside an email, it might be better to use AMPscript instead of SSJS to pull and parse the data from your array 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)
    Output(v(@value))
    
  next @i 
endif ]%%

Adding additional answer from the comments. In case you want to have two separate variables for each of the values, you can do this instead of the loop:

%%[
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

set @row = row(@rows, 1) /* get first value */     
set @value1 = field(@row,1)             
set @row = row(@rows, 2) /* get second value */     
set @value2 = field(@row,1)    
If @value1 == "something" or @value2 == "something" then

]%% <br>some text<br> %%[

endif
endif ]%%

You can use and/or in the condition or do two separate conditions using elseif/else depending on your needs. Read more about using conditional statements here: https://ampscript.guide/statements/

5
  • Yes this works. One question about this script. Here you loop over the array. Is it possible to read the data without the loop? So with this example array ["value1","value2"] in mind, suppose I want to use only 1 value eg only value 2? With the above script you will now always see the fallback (else). And if I remove the fallback and use an OR statement, I get to see my value twice.
    – Martijn e
    Nov 9, 2022 at 12:08
  • If you only want to always use the second value, you can do something like this instead of the loop: set @row = row(@rows, 2) /* get second row only */ set @value = field(@row,1) Output(v(@value))
    – zuzannamj
    Nov 9, 2022 at 12:19
  • It's not that I always want to use the second value. Sometimes only value 1, sometimes only value 2 or a combination of the values. So if I use: if @ value == "value1" then ]%% <br>some text for value 1<br> %%[ else ]%% <br>fallback text for other values<br> %%[ endif It shows value1 but also the fallback. And if I use: if @ value == "value1" or @ value == "value2" then ]%% <br>some text for value 1<br> then it shows both values instead of one of them. Hopefully this make sens to you.
    – Martijn e
    Nov 9, 2022 at 12:53
  • 1
    In that case you can do something like this instead of the loop: set @row = row(@rows, 1) /* get first value */ set @value1 = field(@row,1) set @row = row(@rows, 2) /* get second value */ set @value2 = field(@row,1) If @value1 == "something" or @value2 == "something" then … endif. You can use and/or in the condition or do two separate conditions using elseif/else.
    – zuzannamj
    Nov 9, 2022 at 12:54
  • Ah great. This is what I was looking for. Thanks.
    – Martijn e
    Nov 9, 2022 at 13:08
0

You can combine SSJS and AMPscript, use SSJS to loop and parse the values and then set value into an AMPscript variable to process. For example:

<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>

Output:

value1
value2
4
  • Great. I will give it a try.
    – Martijn e
    Nov 8, 2022 at 7:31
  • Your example worked. But how can I target a data extention where the JSON is in a attribute field?
    – Martijn e
    Nov 8, 2022 at 9:36
  • And can you act on the output? So for exampe, if value1 then show this text, and for value2 another text etc.
    – Martijn e
    Nov 8, 2022 at 9:50
  • @Martijne You would need to know how to retrieve records from a Data Extension. You can also do with other actions depends on what you are going to do. I recommend you should open a new topic if you have further questions
    – Duc Le
    Nov 8, 2022 at 9:59

You must log in to answer this question.

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