Is there any way to convert a custom class eg: dw.customer.CustomerAddress
to JSON format and return the value in a controller?
If I just return the attribute I'm receiving an empty object.
I know I can add each object value and return it, but I need a more automatic way to do that.
This is my code:
server.get('MyMethod', function (req, res, next) {
var customerNo = req.querystring.customerNo;
var profile;
if (customerNo) {
profile = CustomerMgr.searchProfile("customerNo = {0}", customerNo, true);
}
if (profile) {
var addresses = [];
if (!empty(profile.addressBook)) {
profile.addressBook.addresses.toArray()
.forEach((address) => {
addresses.push(JSON.serialize(address));
});
}
res.json({
customerNo: profile.customerNo,
addresses: addresses,
});
} else {
res.json({
errorCode: 404,
errorMessage: "not found"
});
}
delete res.viewData["action"];
delete res.viewData["locale"];
delete res.viewData["queryString"];
next();
});