Strings in data.json

Hi, guys I was trying to import my data from a json file that I use to save some data, but when I try it appears like this ...
import

I check on my Raspberry the data and it is like this...

some help to eliminate the " " on my data? there is my funciton to write data in json file...

var presionlitros = (parseFloat(global.get("presionbar")).toFixed(2));
var humedad=(parseFloat(msg.payload).toFixed(2));
var d = new Date();
var date = d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
var dateTime = date+' '+time;
//var t = dateTime.getTime();
payload={"time":dateTime,"humedad":humedad,"presion":presionlitros};
//msg.payload=JSON.stringify(payload);
//msg.filename="/home/pi/logs/data.log";
msg.payload=payload;
return msg;```

The .json file does not contain json, unless you read it line by line.

Can you post your flow ?

Try using the file in node in per line mode then to a json node.

Sorry I thouthg that there is any functions to convert the string in to text or number.

Here is my flow I just want to eliminate the " " that is writing in every data going in, on the file like I showed in my last post.

[{"id":"1a7bb3c4.55955c","type":"file","z":"fb3bc52f.8ba188","name":"log sensor data","filename":"/home/pi/Desktop/pruebasensores15072020/pruebastiempos2.json","appendNewline":true,"createDir":true,"overwriteFile":"false","x":940,"y":320,"wires":[[]]},{"id":"58c7c1e9.4f25d","type":"json","z":"fb3bc52f.8ba188","name":"","property":"payload","action":"","pretty":false,"x":770,"y":320,"wires":[["1a7bb3c4.55955c"]]},{"id":"b79dcdea.8b9f4","type":"function","z":"fb3bc52f.8ba188","name":"Modify Payload Changed","func":"var local=context.get('local') || {};\nvar topic=msg.topic;\nvar last_payload=\"\";\nif (local[topic]===undefined)//test exists\n{\n    node.log(\"not defined \"+topic);\n    payload=msg.payload;\n}\nelse{\n  \nlast_payload=local[topic];\n}\n\nnode.log(\"last payload \"+ last_payload);\nnode.log(\"This payload \"+ msg.payload);\n//msg.payload=JSON.stringify(payload);\nnode.log(JSON.stringify(local));\n//\nif (msg.payload==last_payload)\n{\nnode.log(\"same data\"+topic);\nreturn null;\n}\nelse{\nlocal[topic]=msg.payload;\nnode.log(local);\ncontext.set('local',local);\n}\n//\nvar d = new Date();\nvar t = d.getTime();\npayload={\"time\":t,\"payload\":msg.payload,\"topic\":msg.topic};\n//msg.payload=JSON.stringify(payload);\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":550,"y":380,"wires":[["58c7c1e9.4f25d"]]},{"id":"95758af4.ca6d58","type":"function","z":"fb3bc52f.8ba188","name":"All data","func":"var getvalueone = global.get(\"valueone\");\nvar getvaluetwo = msg.payload;\nvar d = new Date();\nvar date = d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();\nvar time = d.getHours() + \":\" + d.getMinutes() + \":\" + d.getSeconds();\nvar dateTime = date+' '+time;\n//var t = dateTime.getTime();\npayload={\"time\":dateTime,\"value2\":getvaluetwo,\"value1\":getvalueone};\n//msg.payload=JSON.stringify(payload);\n//msg.filename=\"/home/pi/logs/data.log\";\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":320,"wires":[["58c7c1e9.4f25d"]]},{"id":"b8bd1b34.f1ddd8","type":"function","z":"fb3bc52f.8ba188","name":"","func":"var valueone =msg.payload;\nglobal.set('valueone',valueone);\nmsg.payload=valueone;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":200,"wires":[["42e1282.1cad4d8"]]},{"id":"42e1282.1cad4d8","type":"debug","z":"fb3bc52f.8ba188","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":200,"wires":[]},{"id":"6d2af628.2aebf8","type":"inject","z":"fb3bc52f.8ba188","name":"messagenumerone","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"messagenumerone","payloadType":"str","x":230,"y":200,"wires":[["b8bd1b34.f1ddd8"]]},{"id":"f99e6f41.53352","type":"function","z":"fb3bc52f.8ba188","name":"","func":"var valuetwo =msg.payload;\nmsg.payload=valuetwo;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":440,"y":320,"wires":[["5cc98664.cbf9e8","95758af4.ca6d58"]]},{"id":"f5b693b3.110b7","type":"inject","z":"fb3bc52f.8ba188","name":"messagenumertwo","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"messagenumertwo","payloadType":"str","x":230,"y":320,"wires":[["f99e6f41.53352"]]},{"id":"5cc98664.cbf9e8","type":"debug","z":"fb3bc52f.8ba188","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":260,"wires":[]}]

It is caused by the json node, which is not needed.

try this flow (i have modified the date time format):

[{"id":"71d454c4.353654","type":"function","z":"7a27c490.c177ac","name":"All data","func":"var getvalueone = global.get(\"valueone\");\nvar getvaluetwo = msg.payload;\nvar nd = new Date().toISOString().split(\"T\");\nvar d = nd[0]\nvar t = nd[1].substring(0,8)\nvar dt = d+\" \"+t\nreturn {payload:{\"time\":dt,\"value2\":getvaluetwo,\"value1\":getvalueone}};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":312,"wires":[["580b2c08.19268c"]]},{"id":"d7c86c0.c638b18","type":"function","z":"7a27c490.c177ac","name":"","func":"global.set('valueone',msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":264,"wires":[["f955d7cb.ad88"]]},{"id":"f955d7cb.ad88","type":"debug","z":"7a27c490.c177ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":574,"y":264,"wires":[]},{"id":"fdb9c1d4.004278","type":"inject","z":"7a27c490.c177ac","name":"messagenumerone","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$random()\t","payloadType":"jsonata","x":220,"y":264,"wires":[["d7c86c0.c638b18"]]},{"id":"f69ff355.63e9a8","type":"inject","z":"7a27c490.c177ac","name":"messagenumertwo","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$random()","payloadType":"jsonata","x":220,"y":312,"wires":[["71d454c4.353654"]]},{"id":"580b2c08.19268c","type":"debug","z":"7a27c490.c177ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":574,"y":312,"wires":[]}]

Thanks actually that is what I want just one last question there is a way to eliminate the " " on the date?

I will try this function with my flow

eliminate
Actually I realize that my real problem is the } (marked on with red in the picture) at the end any solution to eliminate that } or add any space or ,

This is in Excel if you want to check it.

Also I noticed that in the function you are using your time zone because I used and it appears a different hour than mine so, I'm a little bit lost in the code, wich part is the timezone set?

var getvalueone = global.get("valueone");
var getvaluetwo = msg.payload;
var nd = new Date().toISOString().split("T");
var d = nd[0]
var t = nd[1].substring(0,8) // I suppose that is this one
var dt = d+" "+t
return {payload:{"time":dt,"value2":getvaluetwo,"value1":getvalueone, }

If you just want to import it into excel, you can use your initial flow and replace the json node with a csv node.

I have a question someone a while ago in a topic that I post to make the flow that I had troubles with, told me that was better json file than csv, but I'm confused so is the same? (about the writing, speed, no problems, size.) Ando algo sorry to ask again so the line to change the timezone because you change the date time format is this one?

var t = nd[1].substring(0,8) // I suppose that is this one

To give you a solution, we need to know what you are trying to do, but that is not explained in this topic, other than your question to remove the quotes " and {}

When I see your screenshot, it seems like you want to use it in excel, if that is what you want, you can use the csv node.

Sorry, you're right so basically I'm saving data to export that to excel, when I started using Node-RED I asked about saving data, so everybody was saying to save data use databases but I had never use databases. So the data that I'm saving is to use in Excel or another software to make graphics and analize so I need to use a format to save this data. Also the timing to write data will be 1 second.

This quotes " and {} give me troubles when I try to import my json file like in the last code that you helped me. the last value is imported like this ....

That's why I asked to eliminate the quotes {}

You can use this flow

[{"id":"733f8b22.bbb37c","type":"file","z":"5008b27.8f1b2cc","name":"log sensor data","filename":"/home/pi/Desktop/pruebasensores15072020/pruebastiempos2.json","appendNewline":false,"createDir":true,"overwriteFile":"false","x":800,"y":408,"wires":[[]]},{"id":"8e07289b.0d29f","type":"function","z":"5008b27.8f1b2cc","name":"Modify Payload Changed","func":"var local=context.get('local') || {};\nvar topic=msg.topic;\nvar last_payload=\"\";\nif (local[topic]===undefined)//test exists\n{\n    node.log(\"not defined \"+topic);\n    payload=msg.payload;\n}\nelse{\n  \nlast_payload=local[topic];\n}\n\nnode.log(\"last payload \"+ last_payload);\nnode.log(\"This payload \"+ msg.payload);\n//msg.payload=JSON.stringify(payload);\nnode.log(JSON.stringify(local));\n//\nif (msg.payload==last_payload)\n{\nnode.log(\"same data\"+topic);\nreturn null;\n}\nelse{\nlocal[topic]=msg.payload;\nnode.log(local);\ncontext.set('local',local);\n}\n//\nvar d = new Date();\nvar t = d.getTime();\npayload={\"time\":t,\"payload\":msg.payload,\"topic\":msg.topic};\n//msg.payload=JSON.stringify(payload);\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":468,"wires":[[]]},{"id":"fd685e89.5a53f","type":"function","z":"5008b27.8f1b2cc","name":"All data","func":"var getvalueone = global.get(\"valueone\");\nvar getvaluetwo = msg.payload;\nvar d = new Date();\nvar date = d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();\nvar time = d.getHours() + \":\" + d.getMinutes() + \":\" + d.getSeconds();\nvar dateTime = date+' '+time;\npayload={\"time\":dateTime,\"value2\":getvaluetwo,\"value1\":getvalueone};\nmsg.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":408,"wires":[["c74fe8d7.bf7a5","8289dbaf.a3a758"]]},{"id":"1e192bb8.b8c4bc","type":"function","z":"5008b27.8f1b2cc","name":"","func":"var valueone =msg.payload;\nglobal.set('valueone',valueone);\nmsg.payload=valueone;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":288,"wires":[["e5ae1637.60781"]]},{"id":"e5ae1637.60781","type":"debug","z":"5008b27.8f1b2cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":288,"wires":[]},{"id":"b30c3c03.adb9b","type":"inject","z":"5008b27.8f1b2cc","name":"messagenumerone","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"messagenumerone","payloadType":"str","x":90,"y":288,"wires":[["1e192bb8.b8c4bc"]]},{"id":"8e39c06f.ac1ae8","type":"function","z":"5008b27.8f1b2cc","name":"","func":"var valuetwo =msg.payload;\nmsg.payload=valuetwo;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":408,"wires":[["fd685e89.5a53f"]]},{"id":"d7c82caa.7fdb48","type":"inject","z":"5008b27.8f1b2cc","name":"messagenumertwo","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"messagenumertwo","payloadType":"str","x":90,"y":408,"wires":[["8e39c06f.ac1ae8"]]},{"id":"c74fe8d7.bf7a5","type":"csv","z":"5008b27.8f1b2cc","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":624,"y":408,"wires":[["733f8b22.bbb37c"]]},{"id":"8289dbaf.a3a758","type":"debug","z":"5008b27.8f1b2cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":348,"wires":[]}]

This writes it as csv, you can directly open it in excel

Thanks actually I used to think that the json node must be always to write in .json files but this make everything as I want.

1 Like

hi bro @bakman2 it appears this I don't know why

"No columns template specified for object -> CSV."

You should really specify a template in the node using the property names so that the cab can be created in the right order you want (and can omit any properties you don’t want) otherwise it will just create it as per the first payload it sees and assumes all following payloads e we I’ll be in the same order with the same properties.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.