# Change a value within a CSV File

**URL:** <https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997>\
**Category:** General\
**Created:** [21 July 2023 13:40 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997 "2023-07-21T13:40:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [21 July 2023 13:40 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/1 "2023-07-21T13:40:29Z")

</div>

So my input csv file that I am reading from has a reading of this:  
e0040150c6cd280d,bolt,5  
e0040150c6cd14c3,screw,0  
e0040150c6cd2a81,nut,3  
e0040150c6cd245b,washer,0

I am struggling to figure out how to write a function that replaces the '0' value with 'null' and not just append to a new line and leave the '0' in the file.  
I want the csv file to look like this when it is written back:

e0040150c6cd280d,bolt,5  
e0040150c6cd14c3,screw,null  
e0040150c6cd2a81,nut,3  
e0040150c6cd245b,washer,null

---

<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:** [21 July 2023 13:58 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/2 "2023-07-21T13:58:09Z")

</div>

Maybe this example will help

```auto
[{"id":"0cc20c51c1ace4cc","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280.00000762939453,"y":3243.000096321106,"wires":[["8ad3c8761acf4695"]]},{"id":"8ad3c8761acf4695","type":"template","z":"b9860b4b9de8c8da","name":"simulate csv file read","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"e0040150c6cd280d,bolt,5\ne0040150c6cd14c3,screw,0\ne0040150c6cd2a81,nut,3\ne0040150c6cd245b,washer,0\n","output":"str","x":500.00000762939453,"y":3243.000096321106,"wires":[["d1d232d57e43aa95"]]},{"id":"d1d232d57e43aa95","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":"","hdrout":"none","multi":"mult","ret":"\\n","temp":"id,name,value","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":310.00000762939453,"y":3303.000096321106,"wires":[["90548984a7c5c51c"]]},{"id":"90548984a7c5c51c","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload ~> |$|$.value = 0 ? {\"value\":null} : {}|","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460.00000762939453,"y":3303.000096321106,"wires":[["229cfe3c397c732a"]]},{"id":"229cfe3c397c732a","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":630.0000076293945,"y":3303.000096321106,"wires":[["66f3a8506ea55dae"]]},{"id":"66f3a8506ea55dae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 323","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490.00000762939453,"y":3363.000096321106,"wires":[]}]

```

The expression looks for value 0 and replaces with null, using the [JSONata transform operator syntaxs.](https://docs.jsonata.org/other-operators#-------transform)

```auto
$$.payload ~> |$|$.value = 0 ? {"value":null} : {}|

```

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [21 July 2023 14:02 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/3 "2023-07-21T14:02:21Z")

</div>

Sadly that returned this inside the csv file:

```auto
e0040150c6cd280d,bolt,5
e0040150c6cd14c3,screw,0
e0040150c6cd2a81,nut,3
e0040150c6cd245b,washer,0
e0040150c6cd14c3,screw,"{""hex_output"":""e0040150c6cd14c3"",""item_name"":""screw"",""quantity"":0}"
e0040150c6cd245b,washer,"{""hex_output"":""e0040150c6cd245b"",""item_name"":""washer"",""quantity"":0}"

```

---

<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:** [21 July 2023 14:05 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/4 "2023-07-21T14:05:30Z")

</div>

Sadly you must be using it wrong, see image

 ![chrome_screenshot_1689948301602](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f50809293f5944d7eacae2f70cbed8703257cf6.png)

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [21 July 2023 14:14 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/5 "2023-07-21T14:14:02Z")

</div>

Ah yes I added a .quantity to the set msg.payload, but now its just outputting the whole csv again.

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

```auto
e0040150c6cd280d,bolt,5
e0040150c6cd14c3,screw,0
e0040150c6cd2a81,nut,3
e0040150c6cd245b,washer,0
e0040150c6cd280d,bolt,5
e0040150c6cd14c3,screw,0
e0040150c6cd2a81,nut,3
e0040150c6cd245b,washer,0

```

---

<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:** [21 July 2023 14:18 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/6 "2023-07-21T14:18:37Z")

</div>

Guessing as you have not supplied an example flow.

If the cvs node is outputting a message per row you would do something like this

```auto
[{"id":"0cc20c51c1ace4cc","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280.00000762939453,"y":3243.000096321106,"wires":[["8ad3c8761acf4695"]]},{"id":"8ad3c8761acf4695","type":"template","z":"b9860b4b9de8c8da","name":"simulate csv file read","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"e0040150c6cd280d,bolt,5\ne0040150c6cd14c3,screw,0\ne0040150c6cd2a81,nut,3\ne0040150c6cd245b,washer,0\n","output":"str","x":500.00000762939453,"y":3243.000096321106,"wires":[["d1d232d57e43aa95","66f3a8506ea55dae"]]},{"id":"d1d232d57e43aa95","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"id,name,value","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":310.00000762939453,"y":3303.000096321106,"wires":[["0eeac0795b12e354"]]},{"id":"66f3a8506ea55dae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 323","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":3360,"wires":[]},{"id":"0eeac0795b12e354","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":3300,"wires":[["99b22e4c09d0aae9"],["2750bb653d171682"]]},{"id":"b47a7d55bb39eb9d","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":450,"y":3360,"wires":[["66f3a8506ea55dae"]]},{"id":"99b22e4c09d0aae9","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"null","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":3300,"wires":[["2750bb653d171682"]]},{"id":"2750bb653d171682","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":290,"y":3360,"wires":[["b47a7d55bb39eb9d"]]}]

```

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [21 July 2023 14:27 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/7 "2023-07-21T14:27:17Z")

</div>

That worked perfectly, sorry about not being able to send the flow, the file is massive. Thank you so much for the help.

---

<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:** [21 July 2023 14:28 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/8 "2023-07-21T14:28:37Z")

</div>

Do not need the whole flow, just relevent nodes and a simulation of file read , similar to my example.

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [21 July 2023 14:35 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/9 "2023-07-21T14:35:37Z")

</div>

Got it will do that for further questions/problems

---

<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:** [4 August 2023 14:36 UTC](https://discourse.nodered.org/t/change-a-value-within-a-csv-file/79997/10 "2023-08-04T14:36:31Z")

</div>

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