In the Marketing Cloud I have an attribute field. In this field a JSON is stored. Like so:

    "Example1": "XXXXX",
    "Example2": "XXXXXX",
    "Example3": "XXXXX"
}, {
    "Example1": "XXXXX",
    "Example2": "XXXXXX",
    "Example3": "XXXXX"

How can I count the number of objects so I can put that information in my email? So based on the example above you will get something like this in the email:

You have 2 orders.

  • 2
    Please go through your past questions and review the answers provided. I see a number of answers which seem of good quality, but which were never marked by you as accepted. Mar 6 at 11:38

1 Answer 1


This is actually super simple, if you use SSJS and the ParseJSON function. The function returns a JS object, and you can easily check it's length property using .length as illustrated below:

VAR @jsonattribute
SET @jsonattribute = '[{"Example1": "XXXXX",    "Example2": "XXXXXX",    "Example3": "XXXXX"}, {    "Example1": "XXXXX",    "Example2": "XXXXXX",    "Example3": "XXXXX"}]'
<script runat="server" language="javascript">
var jsonresponse = Variable.GetValue("@jsonattribute");;
var json = Platform.Function.ParseJSON(jsonresponse);
Write("You have purchased " + json.length + " products.");

Output: You have purchased 2 products.

  • Thanks. This works when I use the exact script of you. However, when I change the script so it actual takes the JSON from the attrbiute field I got a strange result. I alternated your script to this: <script runat="server" language="javascript"> Platform.Load("Core","1"); var json = Platform.Function.ParseJSON("@XXXX"); Write("You have purchased " + json.length + " products."); </script> outcome is 9 (You have purchased 9 products.). Even when I change the JSON to more or less objects. What do I miss?
    – Martijn e
    Mar 6 at 12:26
  • @Martijne - I have updated this script to include Ampscript too. If this still does not work, then the issue is somewhere else, potentially your stored JSON string. Also, you can't reference ampscript variable as you do in your comment above. Mar 6 at 12:29
  • Awesome that did the trick.
    – Martijn e
    Mar 6 at 12:40
  • one question. It works fine, however, if there is only 1 object I would expect 1 but I got a larger number. It looks like the total of all the data fields
    – Martijn e
    Mar 7 at 10:56
  • That depends on the structure of your JSON, @Martijne. It counts the length (number of items) at the level to which you traverse through your JSON tree. In above example, json.length is 2, while json[0].length will give you 3, since you have Example1, Example2, Example3 elements there. Mar 7 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.