Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!

nms.extAccount.load JSON.stringify JSON.parse

Avatar

Level 3
Level 3

I am performing some tests, I have in an external account in the comment section some configuration in object literal

 

o_X_0-1715709630800.png

 

 {
    JWT : { endpoint  :"https://somelink1",
               clientid     :"consent-management",
               grantType  :"client_credentials" },
    CMP : {endpoint      :"https://someotherlink2",
                contentType :"application/json"}
 }

 

In a workflow I am loading the external account object and matching object configuration in the comment section using a regex, while I can JSON parse it, I cannot seem to navigate using dot notation.

 

 

 

var auth    = nms.extAccount.load(39010);
var regex   = /{([^;]*)}/gm;
var authObj = JSON.parse(JSON.stringify(auth.comment.match(regex)));

logInfo(authObj);

 

 

 

o_X_1-1715710013904.png

 

Normally I would then navigate using dot notation (authObj.JWT.endpoint) but I am getting undefined.

 

4 Replies

Avatar

Level 7

Which type object is the variable you're trying to access? Print it to check it's the right one

Avatar

Employee Advisor

Hi o_X,

 

there are two issues with your approach:

 

1. code is wrong. Look at this line:

var authObj = JSON.parse(JSON.stringify(auth.comment.match(regex)));

the "match" method returns an array of found matches (in your case 1), like this:

 

[{     JWT : { endpoint  :"https://somelink1",                clientid     :"consent-management",                grantType  :"client_credentials" },     CMP : {endpoint      :"https://someotherlink2",                 contentType :"application/json"} }]

 

 Then your performing a stringify on that Array, which results in this 

"["{\u000a    JWT : { endpoint  :\"https://somelink1\",\u000a               clientid     :\"consent-management\",\u000a               grantType  :\"client_credentials\" },\u000a    CMP : {endpoint      :\"https://someotherlink2\",\u000a                contentType :\"application/json\"}\u000a}"]"

 

Then your JSON.parsing that thing and you again get the same array as the result from the first step.

Instead you should modify you code to this (no JSON stringify needed and you access the first found match):

var auth    = nms.extAccount.load(28470478);
var regex   = /{([^;]*)}/gm; 
var matches = auth.comment.match(regex); // returns array
var authObj = JSON.parse(matches[0]); // access first match and remove JSON.stringify

logInfo('authObj.JWT.endpoint: ' + authObj.JWT.endpoint);
logInfo('authObj.CMP.contentType: ' + authObj.CMP.contentType);

 

2. problem: the JSON in the external account's comment is invalid and the parser isn't able to parse it. You noted:

{
    JWT : { endpoint  :"https://somelink1",
               clientid     :"consent-management",
               grantType  :"client_credentials" },
    CMP : {endpoint      :"https://someotherlink2",
                contentType :"application/json"}
 }

Instead you need to add double quotes around the keys and remove the spaces between the key and the colon:

{
  "JWT": { 
    "endpoint": "https://somelink1",
    "clientid": "consent-management",
    "grantType": "client_credentials"
  },
  "CMP": {
    "endpoint": "https://someotherlink2",
    "contentType": "application/json"
  }
}

 Best regards, Tobias

Avatar

Level 3
Level 3

Hi Tobias,

 

In the case of geting a string from platform options, how do you get the timestamp of lastLog

 

o_X_0-1716819725175.png

 

I've tried

 

var data    = getOption("NmsTracking_Pointer");
var doc     = new DOMDocument.fromXMLString(data)
logInfo(doc.root.getElementsByTagName('lastLog')[0])

 

Avatar

Community Advisor

Hello @o_X,

i've had the same need in the past, here is how i did it : 

 

//Get lastLog Date from NmsTracking_Pointer option
var trackingOption = getOption('NmsTracking_Pointer');
var trackingOptionXML = new XML (trackingOption.replace("<?xml version='1.0'?>",""));
var dateLastLog = parseTimeStamp(trackingOptionXML.server.@lastLog);

 

Br,