# Read/write json revisited

**URL:** <https://discourse.nodered.org/t/read-write-json-revisited/36203>\
**Category:** General\
**Created:** [19 November 2020 17:47 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203 "2020-11-19T17:47:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [19 November 2020 17:47 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/1 "2020-11-19T17:47:54Z")

</div>

Hi,

I'm using text input (set as time picker) to store hours and minutes to a json file. This part works (well...).  
Then when the web page is loaded/reloaded, I need to read the json file and set the text input to the saved time.

So, I'm having a few issues:

1. I cannot get to parse the json data to manipulate it.
2. I don't know how to read said file when the web page is loaded.
3. I'm now storing hour and minutes, but what if the file contains more data like:

> {"led": on, "relay": off, "hour":12, "minute":15}

I really appreciate some assistance on this.  
TIA

```auto
[{"id":"e3f00cd4.c13ba8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d084f6b3.0b2fe8","type":"debug","z":"e3f00cd4.c13ba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":220,"wires":[]},{"id":"60686e6d.c19ee8","type":"ui_text_input","z":"e3f00cd4.c13ba8","name":"webTime","label":"Hora persiana","tooltip":"","group":"e624b78b.0592d8","order":2,"width":"3","height":"1","passthru":false,"mode":"time","delay":"400","topic":"","x":100,"y":140,"wires":[["f88152f4.dd1"]]},{"id":"f88152f4.dd1","type":"function","z":"e3f00cd4.c13ba8","name":"extractTime","func":"var time = msg.payload;\nvar minutes = time / 60000;\nvar hour = 0;\nif (minutes > 59) {\n hour = Math.floor(minutes / 60)\n minutes = minutes % 60\n}\nmsg.payload = {\"hour\":hour, \"minute\":minutes};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":250,"y":140,"wires":[["6402657c.6bc40c"]]},{"id":"c672fbec.bf17d8","type":"file","z":"e3f00cd4.c13ba8","name":"","filename":"/home/pi/test.log","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":540,"y":140,"wires":[[]]},{"id":"6402657c.6bc40c","type":"json","z":"e3f00cd4.c13ba8","name":"","property":"payload","action":"","pretty":false,"x":390,"y":140,"wires":[["c672fbec.bf17d8"]]},{"id":"b1db9cb4.baf77","type":"inject","z":"e3f00cd4.c13ba8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":220,"wires":[["9d14a4c.cb751d8"]]},{"id":"9d14a4c.cb751d8","type":"file in","z":"e3f00cd4.c13ba8","name":"","filename":"/home/pi/test.log","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":290,"y":220,"wires":[["fe5c13a5.c61c6"]]},{"id":"fe5c13a5.c61c6","type":"function","z":"e3f00cd4.c13ba8","name":"timeConvert","func":"var m=msg.payload;\nvar hr=JSON.parse(m.hour);\nvar min=JSON.parse(x.minute);\nvar time=((hr*60)+(min))*60000;\nmsg.payload=time;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":220,"wires":[["d084f6b3.0b2fe8","60686e6d.c19ee8"]]},{"id":"e624b78b.0592d8","type":"ui_group","z":"","name":"Mnto","tab":"2332b0a1.2f261","order":3,"disp":true,"width":"6","collapse":true},{"id":"2332b0a1.2f261","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

 ![Cap](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf31dcd7140fa4d2ca0097bef1209346560be1bc.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 November 2020 18:01 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/2 "2020-11-19T18:01:46Z")

</div>

> [@ebolisa](#):
>
> I cannot get to parse the json data to manipulate it.

The JSON node will turn a string of JSON back into a JS object

> [@ebolisa](#):
>
> I don't know how to read said file when the web page is loaded

Use the UI\_Control node to let you know when page is loaded

> [@ebolisa](#):
>
> I'm now storing hour and minutes, but what if the file contains more data like

If it stores more data - dont use it - ignore it!

Based on your comments it seems to me you might not be quite getting JSON / JS Objects.

This will explain how to use the debug panel to find the right path to any data item.

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [19 November 2020 19:18 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/3 "2020-11-19T19:18:23Z")

</div>

> [@Steve-Mcl](#):
>
> The JSON node will turn a string of JSON back into a JS object

This' the first obstacle.

```auto
var m=msg.payload 
//var hr=JSON.parse(m.hour);
//var min=JSON.parse(x.minute);
//var time=((hr*60)+(min))*60000;
msg.payload=m;
return msg; //returns {"hour":12,"minute":00}

```

but

> var m=msg.payload  
> var hr=JSON.parse(m.hour);  
> //var min=JSON.parse(x.minute);  
> //var time=((hr_60)+(min))60000;  
> msg.payload=hr;  
> return msg; //_\* fails

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 November 2020 19:38 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/4 "2020-11-19T19:38:20Z")

</div>

Try this example

```auto
[{"id":"26dffcb.5654f84","type":"ui_text_input","z":"a1dea2b.76223e","name":"webTime","label":"Hora persiana","tooltip":"","group":"f7409047.72d11","order":2,"width":"3","height":"1","passthru":true,"mode":"time","delay":"400","topic":"","x":100,"y":140,"wires":[["ded6e057.f6c1c8","f580a7a9.8e6188"]]},{"id":"ded6e057.f6c1c8","type":"function","z":"a1dea2b.76223e","name":"extractTime","func":"var time = msg.payload;\nvar hour = Math.floor(time / 3600000);\nvar minutes = Math.round((time % 3600000) / 60000);\nmsg.payload = {\"hour\":hour, \"minute\":minutes};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":300,"y":120,"wires":[["f9251ea2.35ea8","f580a7a9.8e6188"]]},{"id":"44fb75fe.12ae14","type":"function","z":"a1dea2b.76223e","name":"timeConvert","func":"var m=msg.payload;\nvar time=((m.hour*3600000)+(m.minute*60000));\nmsg.payload=time;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":220,"wires":[["26dffcb.5654f84","f580a7a9.8e6188"]]},{"id":"f580a7a9.8e6188","type":"debug","z":"a1dea2b.76223e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":300,"wires":[]},{"id":"f9251ea2.35ea8","type":"json","z":"a1dea2b.76223e","name":"","property":"payload","action":"","pretty":false,"x":450,"y":120,"wires":[["f580a7a9.8e6188"]]},{"id":"1459ca5e.bb38fe","type":"json","z":"a1dea2b.76223e","name":"","property":"payload","action":"","pretty":false,"x":350,"y":220,"wires":[["44fb75fe.12ae14","f580a7a9.8e6188"]]},{"id":"c1397013.69a42","type":"inject","z":"a1dea2b.76223e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"hour\":12,\"minute\":57}","payloadType":"str","x":140,"y":220,"wires":[["1459ca5e.bb38fe","f580a7a9.8e6188"]]},{"id":"f7409047.72d11","type":"ui_group","z":"","name":"Mnto","tab":"c84fe82d.fb3f58","order":3,"disp":true,"width":"6","collapse":true},{"id":"c84fe82d.fb3f58","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [19 November 2020 20:35 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/6 "2020-11-19T20:35:57Z")

</div>

It worked, thank you much!!

Just one more question:

The write-to-file node has the option to overwrite or to append. But what if I want to modify the data? Should I read it first, modify it and then overwite the old data with the new one? Or is there a way to modify just the corrected data, hour and minute, in this case?

Thanks again.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 November 2020 20:46 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/7 "2020-11-19T20:46:26Z")

</div>

Yes as it has to be valid json, if you append you would create a non valid json.  
so read json, change values, then overwrite.

> [@ebolisa](#):
>
> Or is there a way to modify just the corrected data, hour and minute, in this case

Not with text files, you could use context storage or a data base, then you could target just the changing variables.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 December 2020 20:46 UTC](https://discourse.nodered.org/t/read-write-json-revisited/36203/8 "2020-12-03T20:46:33Z")

</div>

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