node-red transform json to csv – two variants

Written by:

the goal is to convert a json object to csv format (in two different variants) and add today’s date.

Starting points are a json structure like this:

{
    "key1": 600,
    "key2": 500,
    "key3": 700
}

Option 1 uses the standard CSV node and a function node to add today’s date:

let loadinfo = [msg.payload];
var d = new Date();
var dt = d.toLocaleString("no-NO");
let updatedloadinfo = [];

for (let i = 0; i < loadinfo.length; i++) {
    loadinfo[i].dato = dt;
  
    updatedloadinfo.push(loadinfo[i]);
}

// Send the updated message
node.send({ payload: updatedloadinfo })

Option 2 creates a csv result with a function node e.g

// Given JSON object

let data = msg.payload;

// Get today's date
let today_date = new Date().toISOString().split('T')[0];

// Prepare the CSV data
let csv_data = "date,key,value\n";

// Add each key-value pair to the CSV data with today's date
for (let key in data) {
    if (data.hasOwnProperty(key)) {
        csv_data += `${today_date},${key},${data[key]}\n`;
    }
}

// Output the CSV data
msg.payload = csv_data;
return msg;

Complete flow

[
    {
        "id": "1c1f48291067acef",
        "type": "group",
        "z": "84f1319303339535",
        "name": "json2csv",
        "style": {
            "fill": "#ffff3f",
            "label": true
        },
        "nodes": [
            "3ad7f58ddca8ec97",
            "7022ec85b96dc378",
            "334617d84ddd4980",
            "99f7b73c36819808",
            "ad5dca6690f88c3c",
            "cae959ae33a667d5",
            "8055d63dc7522ec0"
        ],
        "x": 34,
        "y": 79,
        "w": 1052,
        "h": 162
    },
    {
        "id": "3ad7f58ddca8ec97",
        "type": "function",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "tocsv",
        "func": "// Given JSON object\n\nlet data = msg.payload;\n\n// Get today's date\nlet today_date = new Date().toISOString().split('T')[0];\n\n// Prepare the CSV data\nlet csv_data = \"date,key,value\\n\";\n\n// Add each key-value pair to the CSV data with today's date\nfor (let key in data) {\n    if (data.hasOwnProperty(key)) {\n        csv_data += `${today_date},${key},${data[key]}\\n`;\n    }\n}\n\n// Output the CSV data\nmsg.payload = csv_data;\nreturn msg;\n\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 200,
        "wires": [
            [
                "334617d84ddd4980"
            ]
        ]
    },
    {
        "id": "7022ec85b96dc378",
        "type": "debug",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "debug 507",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 970,
        "y": 180,
        "wires": []
    },
    {
        "id": "334617d84ddd4980",
        "type": "file",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "",
        "filename": "c:\\mon\\output\\tables2.csv",
        "filenameType": "str",
        "appendNewline": true,
        "createDir": false,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 560,
        "y": 200,
        "wires": [
            [
                "7022ec85b96dc378"
            ]
        ]
    },
    {
        "id": "99f7b73c36819808",
        "type": "inject",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "full-array",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"key1\":600,\"key2\":500,\"key3\":700}",
        "payloadType": "json",
        "x": 140,
        "y": 160,
        "wires": [
            [
                "3ad7f58ddca8ec97",
                "8055d63dc7522ec0"
            ]
        ]
    },
    {
        "id": "ad5dca6690f88c3c",
        "type": "csv",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "all",
        "multi": "one",
        "ret": "\\n",
        "temp": "key1,key2,key3,dato",
        "skip": "0",
        "strings": true,
        "include_empty_strings": true,
        "include_null_values": true,
        "x": 550,
        "y": 120,
        "wires": [
            [
                "cae959ae33a667d5"
            ]
        ]
    },
    {
        "id": "cae959ae33a667d5",
        "type": "file",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "",
        "filename": "c:\\mon\\output\\tables1.csv",
        "filenameType": "str",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 800,
        "y": 120,
        "wires": [
            [
                "7022ec85b96dc378"
            ]
        ]
    },
    {
        "id": "8055d63dc7522ec0",
        "type": "function",
        "z": "84f1319303339535",
        "g": "1c1f48291067acef",
        "name": "add-date-to-array",
        "func": "let loadinfo = [msg.payload];\nvar d = new Date();\nvar dt = d.toLocaleString(\"no-NO\");\nlet updatedloadinfo = [];\n\nfor (let i = 0; i < loadinfo.length; i++) {\n    loadinfo[i].dato = dt;\n  \n    updatedloadinfo.push(loadinfo[i]);\n}\n\n// Send the updated message\nnode.send({ payload: updatedloadinfo })",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 120,
        "wires": [
            [
                "ad5dca6690f88c3c"
            ]
        ]
    }
]

Discover more from Node-RED LoRaWAN CouchDB and more

Subscribe to get the latest posts sent to your email.

Leave a comment