Dynamic variable help

Hi. I’m fairly new to Postman and hit a wall. It’s possible I have more than 1 issue so I will try to be as detailed as possible. Please let me know if I don’t include enough info.
I’m attempting to build a list as a variable using utils in the request level pre-request script and within that utils I’m attempting to set the date/time using $isoTimestamp. When I send the request, the response body I get back is
{
“title”: “HttpMessageNotReadableException”,
“detail”: “Malformed JSON request”
}
Here is my request level pre-request script (there are more attributes but I’ve shortened the list for simplicity):
pm.variables.replaceIn("{{$isoTimestamp}}");

var transactionObjectList = [
utils.buildTransactionObject( {
payAmount: 0.00,
transactionId: “transactionId”,
transactionDateTime: “$isoTimestamp”,
})
]

pm.collectionVariables.set(“transactionObjectList”, transactionObjectList);
pm.collectionVariables.set(“transactionAmount”, 123.45);

and here is the Body:
{
“eventSource”: {{pm.request.name}},
“eventDateTime”: {{$isoTimestamp}},
“eventObject”: {},
“transactions”: {{transactionObjectList}}
}

My 1st question is what is the proper way to use/set $isoTimestamp? (I realize I probably have something extra going on in this code)
My 2nd question is what causes the Malformed JSON request error and how might I correct that?

**Edit: apologies I left out a part. These requests are in a subfolder so in the pre-request script of that subfolder, I have:
utils = {
buildTransactionObject: function({ payAmount = 0.00, transactionId = undefined, transactionDateTime = undefined} = {}) {
var transactionObject = {};
transactionObject.payAmount = payAmount;
transactionObject.transactionId = transactionId;
transactionObject.transactionDateTime = transactionDateTime;

    return transactionObject;
}

}

json accept data types like number, object , array and string.

string should be enclosed with double quotes , single is not supported so

{
“eventSource”: "{{pm.request.name}}",
“eventDateTime”: "{{$isoTimestamp}}",
“eventObject”: {},
“transactions”: {{transactionObjectList}}
}

if transactionobjectlist is an array or object you don’t need to enclose it with quotes elese you have to

3 Likes

thanks. that did help. I was also missing the braces around $isostamp where I call transactionObjectList.
so, I had
transactionDateTime: “$isoTimestamp”,
but it needed to be
transactionDateTime: “{{$isoTimestamp}}”,
that resolved the syntax to use $isoTimestamp the way I wanted but I was still getting the Malformed JSON request error. after some experimenting and getting help from a dev, we came up with this which resolved the issue and gave me the success response I was looking for
pm.collectionVariables.set(“transactionObjectList”, JSON.stringify(transactionObjectList));