Home > Mobile >  How do I write a json list with objects in javascript
How do I write a json list with objects in javascript

Time:08-27

Hello Guys I have a small problem with writing my list.

I got this out of the debug tab and i only want to have one output without undefind.

Debug-Tab output

I used Node-Red this is my code

Node-Red Code

This is the code of the function Set Data

var number1 = msg.payload.kinput
var number2 = msg.payload.kinout

var newlisencenumber = '{'   '"number1json"'   ':'   number1  ',' '"number2json"' ':'  number2  '}'

//-------------------------------------------------------------
var prepared = '{'   '"storage"'   ':'   '['  newlisencenumber   ']'   '}'

//-------------------------------------------------------------
var msgout = prepared


 


msg.payload = msgout

return msg;

At least here is the code for the Node-Red flow

[
    {
        "id": "b071a31b549f930b",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "1b26b618f4eb0d4e",
        "type": "inject",
        "z": "b071a31b549f930b",
        "name": "",
        "props": [
            {
                "p": "payload.kinput",
                "v": "\"A1234\"",
                "vt": "str"
            },
            {
                "p": "payload.kinout",
                "v": "\"in\"",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 270,
        "y": 300,
        "wires": [
            [
                "45ab02267afb7af9",
                "ede6741db6b1fc08"
            ]
        ]
    },
    {
        "id": "ede6741db6b1fc08",
        "type": "function",
        "z": "b071a31b549f930b",
        "name": "Set Data",
        "func": "var number1 = msg.payload.kinput\nvar number2 = msg.payload.kinout\n\nvar newlisencenumber = '{'   '\"licensenumber\"'   ':'   number1  ',' '\"inout\"' ':'  number2  '}'\n\n//-------------------------------------------------------------\nvar prepared = '{'   '\"storage\"'   ':'   '['  newlisencenumber   ']'   '}'\n\n//-------------------------------------------------------------\nvar msgout = prepared\n\n\n \n\n\nmsg.payload = msgout\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 840,
        "y": 360,
        "wires": [
            [
                "5cbe08fbd89b1b22"
            ]
        ]
    },
    {
        "id": "77a032844ea6a095",
        "type": "inject",
        "z": "b071a31b549f930b",
        "name": "",
        "props": [
            {
                "p": "payload.kinput",
                "v": "A1234",
                "vt": "str"
            },
            {
                "p": "payload.kinout",
                "v": "out",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 270,
        "y": 380,
        "wires": [
            [
                "45ab02267afb7af9",
                "ede6741db6b1fc08"
            ]
        ]
    },
    {
        "id": "d8a6455136577088",
        "type": "inject",
        "z": "b071a31b549f930b",
        "name": "",
        "props": [
            {
                "p": "payload.kinput",
                "v": "A4321",
                "vt": "str"
            },
            {
                "p": "payload.kinout",
                "v": "in",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 270,
        "y": 340,
        "wires": [
            [
                "45ab02267afb7af9",
                "ede6741db6b1fc08"
            ]
        ]
    },
    {
        "id": "f6a8cc6165483098",
        "type": "inject",
        "z": "b071a31b549f930b",
        "name": "",
        "props": [
            {
                "p": "payload.kinput",
                "v": "A4321",
                "vt": "str"
            },
            {
                "p": "payload.kinout",
                "v": "out",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 270,
        "y": 420,
        "wires": [
            [
                "45ab02267afb7af9",
                "ede6741db6b1fc08"
            ]
        ]
    },
    {
        "id": "45ab02267afb7af9",
        "type": "file in",
        "z": "b071a31b549f930b",
        "name": "",
        "filename": "C:\\test\\test.json",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 500,
        "y": 360,
        "wires": [
            [
                "ede6741db6b1fc08"
            ]
        ]
    },
    {
        "id": "5cbe08fbd89b1b22",
        "type": "debug",
        "z": "b071a31b549f930b",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 360,
        "wires": []
    }
]

CodePudding user response:

Your code is typecasting the values to string. Now in this case undefined values will convert to a string "undefined".

You could do something like this:

// Basically add a fallback of empty string
var number1 = msg?.payload?.kinput || "";
var number2 = msg?.payload?.kinout || "";

So if the value is undefined or even null it will fallback to an empty string so you should get something like this

"{"storage": [{ "licensenumber": "", "input": "" }]}"
  • Related