1

I want to get attribute sets for an attribute group. So before I used an API call just for the attribute groups. And it worked. For this I make the API GET call and it is successful but I can not parse it. If I just put the response.content in a write function it displays but when I add the ParseJSON function I get the try catch block error. The JSON file is a bit large and for this code I get "invalid length" error.

  1. This is the code.

     <script runat="server">
     Platform.Load("Core", "1");
     try {
     var authEndpoint = 
     'https://mcx3g81x4fmdkj05rbw0jg9yb19m.auth.marketingcloudapis.com/';
     var payload = {
     client_id: "xxxxxxxxxxxxxxxxxx",
     client_secret: "xxxxxxxxxxxxxx",
     grant_type: "client_credentials" ,
     account_id: "xxxxxxxx"};
     var url = authEndpoint + '/v2/token'
     var contentType = 'application/json'
    
    
    
     var accessTokenRequest = HTTP.Post(url, contentType, 
     Stringify(payload));
     //Write(accessTokenRequest.StatusCode);
     if (accessTokenRequest.StatusCode == 200) {
     var tokenResponse = 
     Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
     var accessToken = tokenResponse.access_token
     var rest_instance_url = tokenResponse.rest_instance_url};
     var auth = 'Bearer ' + accessToken;
    
     var output = [];
     var page = 1;
     var pageSize = 50;
     var requestUrl = rest_instance_url + 
     "/contacts/v1/attributeSetDefinitions?$page=" + page + "&$pageSize" 
     + pageSize;
    
    
     var req = new Script.Util.HttpRequest(requestUrl);
     req.emptyContentHandling = 0;
     req.retries = 2;
     req.continueOnError = true;
     req.contentType = "application/json"
     req.setHeader("Authorization",auth );
     req.method = "GET";
     req.encoding= "UTF-8"
    
    
    
    var resp = req.send();
    //Write(String(resp.content))
    var result = String(resp.content)
    var obj = Platform.Function.ParseJSON(result);
    Write(obj.items)
    
    
    
    
    
    
    // Check the response code
    if(resp.statusCode == 200) {
    
    
    //var obj = Platform.Function.ParseJSON(String(resp.content))
    
    
    
    
    }
    
    
    
    } catch (error) {
    Write("ERRORBIG"+error);
    }
    </script>
    
4
  • 1
    Welcome to SFSE (Salesforce StackExchange), Ronaldo. I don't know much yet about Marketing Cloud but can I suggest you to format the code in the post instead of sharing the screenshot? Nov 17 at 9:39
  • Thanks I changed it. Nov 17 at 11:07
  • Can you share what 'try...catch' error you see? Also have you validated that the returned content is in valid JSON format? Can check it here: jsonlint.com Nov 17 at 14:07
  • The error I get is "invalid length". Nov 18 at 11:08
-2

ParseJSON - Parses the supplied string or array value and returns a object from that value. This function will not execute functions within the supplied values. Try writing string function separately.

6
  • I tried that to but it doesn't work. It gives the same error. Nov 17 at 10:59
  • Try writing a custom logic to get value of items from reposne.content.
    – ADITYA S.
    Nov 17 at 11:19
  • I did but it doesn't display anything. Nov 17 at 12:19
  • This is not running any functions inside of the ParseJSON and you can utilize multiple different nested capabilities and functions inside of ParseJSON - it will run based on the final result of the nested actions. Nov 17 at 14:04
  • @Gortonington I'm not sure what you mean by your comment, but I tried the API request op posted and indeed it's not possible to correctly parse the resulting JSON. I tried with smaller page sizes, and then parsing works (eg. page size=5) but then each consecutive page gives the same results (starting from the beginning)
    – zuzannamj
    Nov 18 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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