I'm trying to pass a java script object generated by my red node flow to JSON format. But it is causing me difficulty in knowing how to do it. The object script that is obtained is an hour and minute that is written on the screen for example "13:02". In the "result" screen I need to see in JSON format
{"time": "hh: mm"}
But I get "hh:mm" on the screen but i think not in json.
Also when I submit the URL to a client web service and I try to check it the result in JSON I get this error: There was an error parsing JSON data
This is the code:
msg.headers = {"Content-type" : "application/json"}
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
if(hour.toString().length == 1) {
var hour = '0'+hour;}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
msg.payload = hour+':'+minute;
JSON.stringify({"time": msg.payload});
return msg;`
the debug message show me it is s string:
If you want to return a JSON object then you shouldn't be using the JSON.stringify()
function at all. Just assemble the object you want to send in the payload field.
msg.headers = {"Content-type" : "application/json"}
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
msg.payload = {
"time": hour+':'+minute
}
return msg;
Also if you do need to convert objects to strings and vice versa us the JSON node from the pallet which converts msg.payload
.