Help needed parsing csv as json and get sum of them

I'm trying to parse a csv file
I can parse it like this but I cannot get sum of all "Total"
it has 8 entries on this example but it can be 1 or 19 sometimes
can someone point me to the right direction


:v:

[{"id":"36ab5c4e.68d624","type":"inject","z":"bef2a8fc.1651e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":180,"wires":[["40d281a5.9a3838"]]},{"id":"40d281a5.9a3838","type":"http request","z":"bef2a8fc.1651e","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://raw.githubusercontent.com/umtksa/sturd/main/you.csv","tls":"","persist":false,"proxy":"","authType":"","x":530,"y":180,"wires":[["18c8f9e9.86a7d6"]]},{"id":"18c8f9e9.86a7d6","type":"csv","z":"bef2a8fc.1651e","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":700,"y":180,"wires":[["cdd292c1.31729","308ab05e.88bc78"]]},{"id":"cdd292c1.31729","type":"function","z":"bef2a8fc.1651e","name":"tedd","func":"var value = sum(payload.Total);\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":180,"wires":[["998e46bf.b0182"]]},{"id":"308ab05e.88bc78","type":"debug","z":"bef2a8fc.1651e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":890,"y":60,"wires":[]},{"id":"998e46bf.b0182","type":"debug","z":"bef2a8fc.1651e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":180,"wires":[]}]

Hi Umtksa,

Try this in your function node :

let total = 0;

msg.payload.forEach( el => {

    total += Number(el.Total.replace("$",""))
})

msg.payload = total

return msg;

[EDIT] basically with this, we loop with forEach through all the elements (el) of your array
do some processing to replace the dollar sign and convert to a number ready to be added to the total variable we defined at the start.

thank you for your help,
with your code I can can get sum of total and quantity with seperate function
then I try to do it inside one function and push them to a template node to show on dashboard but I'm failing somehow :confused:

[{"id":"36ab5c4e.68d624","type":"inject","z":"bef2a8fc.1651e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":180,"wires":[["40d281a5.9a3838"]]},{"id":"40d281a5.9a3838","type":"http request","z":"bef2a8fc.1651e","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://raw.githubusercontent.com/umtksa/sturd/main/you.csv","tls":"","persist":false,"proxy":"","authType":"","x":530,"y":180,"wires":[["18c8f9e9.86a7d6"]]},{"id":"18c8f9e9.86a7d6","type":"csv","z":"bef2a8fc.1651e","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":710,"y":180,"wires":[["cdd292c1.31729","1c1d78f2.1a653f","91ebdb17.4bf1d8"]]},{"id":"cdd292c1.31729","type":"function","z":"bef2a8fc.1651e","name":"total","func":"let total = 0;\n\nmsg.payload.forEach( el => {\n\n    total += Number(el.Total.replace(\"$\",\"\"))\n})\n\nmsg.para = \"$\"+total\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":160,"wires":[[]]},{"id":"1c1d78f2.1a653f","type":"function","z":"bef2a8fc.1651e","name":"quantity","func":"let total = 0;\n\nmsg.payload.forEach( el => {\n\n    total += Number(el.Quantity)\n})\n\nmsg.adet = total\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":880,"y":220,"wires":[[]]},{"id":"91ebdb17.4bf1d8","type":"function","z":"bef2a8fc.1651e","name":"all","func":"let total = 0;\n\nmsg.payload.forEach( el => {\n\n    total += Number(el.Total.replace(\"$\",\"\"))\n})\n\nmsg.para = \"$\"+total\n\nreturn msg;\n\n\nlet totalq = 0;\n\nmsg.payload.forEach( el => {\n\n    totalq += Number(el.Quantity)\n})\n\nmsg.adet = totalq\n\nvar msg1 = { payload:\"$\"+total };\nvar msg2 = { payload:totalq };\n\nreturn msg1,msg2;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":300,"wires":[["9ac44dc9.c8d498"]]},{"id":"9ac44dc9.c8d498","type":"ui_template","z":"bef2a8fc.1651e","group":"80156726.70f348","name":"herita intagram","order":3,"width":3,"height":4,"format":"<style>\np.takip{\n    font-size: 35px;\n    letter-spacing: 1px;\n}\np.name1{\n    padding-top:10px;\n    font-size: 28px;\n}\n.testcontainer {\n\ntext-align:center;\n}\n</style>\n \n <div class=\"md-card\">\n  <div class=\"testcontainer\">\n    <p class=\"name1\"><b>{{msg1.payload}}</b></p>  \n    <p class=\"takip\"><b>{{msg2.payload}}</b></p>\n  </div>\n</div> ","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":1060,"y":300,"wires":[[]]},{"id":"80156726.70f348","type":"ui_group","name":"yo","tab":"73f0499a.936ce","order":1,"disp":false,"width":13,"collapse":false},{"id":"73f0499a.936ce","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]

The problem was that you were returning your msg before finishing the rest of the calculations.
Do the return at the end

[{"id":"36ab5c4e.68d624","type":"inject","z":"42c8e9e3.fd4a18","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":540,"wires":[["40d281a5.9a3838"]]},{"id":"40d281a5.9a3838","type":"http request","z":"42c8e9e3.fd4a18","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://raw.githubusercontent.com/umtksa/sturd/main/you.csv","tls":"","persist":false,"proxy":"","authType":"","x":430,"y":540,"wires":[["18c8f9e9.86a7d6"]]},{"id":"18c8f9e9.86a7d6","type":"csv","z":"42c8e9e3.fd4a18","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":610,"y":540,"wires":[["91ebdb17.4bf1d8"]]},{"id":"91ebdb17.4bf1d8","type":"function","z":"42c8e9e3.fd4a18","name":"all","func":"let total = 0;\n\nmsg.payload.forEach( el => {\n\n    total += Number(el.Total.replace(\"$\",\"\"))\n})\n\nmsg.total = \"$\" + total\n\n//return msg;\n\n\nlet totalq = 0;\n\nmsg.payload.forEach( el => {\n\n    totalq += Number(el.Quantity)\n})\n\nmsg.totalq = totalq\n\n//var msg1 = { payload:\"$\"+total };\n//var msg2 = { payload:totalq };\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":770,"y":540,"wires":[["9ac44dc9.c8d498"]]},{"id":"9ac44dc9.c8d498","type":"ui_template","z":"42c8e9e3.fd4a18","group":"80156726.70f348","name":"herita intagram","order":3,"width":"11","height":4,"format":"<style>\np.takip{\n    font-size: 35px;\n    letter-spacing: 1px;\n}\np.name1{\n    padding-top:10px;\n    font-size: 28px;\n}\n.testcontainer {\n\ntext-align:center;\n}\n</style>\n \n <div class=\"md-card\">\n  <div class=\"testcontainer\">\n    <p class=\"name1\"><b>Total : {{msg.total}}</b></p>  \n    <p class=\"takip\"><b>Quantity {{msg.totalq}}</b></p>\n  </div>\n</div> ","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":960,"y":540,"wires":[[]]},{"id":"80156726.70f348","type":"ui_group","name":"yo","tab":"73f0499a.936ce","order":1,"disp":false,"width":13,"collapse":false},{"id":"73f0499a.936ce","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]

Aahh! yep I see thank you very much for your help

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