How to access msg object per position

Hello,

I have a payload that looks that way:

payload: array[8]
0: object

  • Feldkennung: 104101
  • Feldname: "Projektnummer"
  • WertimFeld: "123 dummy"

I would like to access the values without knowledge of the payload object.

with msg.payload[x].Feldname i can get one spezific value.

I would like to have something like that: msg.payload[x].[y]

I have searched for a solution but i didnt find one.

maybe someone has a clue?

Dearly
Chorum

If y is a variable containing the name then you are nearly there, just take the dot out.
msg.payload[x][y]

Thats my Problem, the names could change (are unknown/different amounts of variables) and thats why i would like to use the position.

If you don't know what the payload is how do you expect to use the values?

In other words, What are you doing with the data?

Its the answer of an "dynamic" sql query. I ask for Objectdata and get answers from a pool of different datasets.
for instance: Objecttype Tire (diamater) , Objecttype Table (H,W,D/ amount tablelegs/ color ...)

So different amount of fields and different names.

The Answer is a list of Datasets with the same search criteria.

On the list i use checkboxes to filter the objects i want to use.
After that i put them on a html document for printing.

But i only need an access to the values without valuenames. :slight_smile:
Like with an array...

Atm i build a solution with asking for all the names to use them. But i hope there is a simplier solution.

Then you need to map the results and grab the key values.

Look up map and Object.values() on the mdn website.

Something like below in a function node after the SQL node...

msg.payload = msg.payload.map(e=> {
  return [...Object.values(e)]
})
return msg;

(Untested/written on phone)

Thanks !
Didnt work with map till today. I will try and will respond after that.

Works like a charm, thank you!

Hello,

i need to go a little bit more further...

With :
werte = msg.payload.map(e=> { return [...Object.values(e)] })

And:
namen = msg.payload.map(e=> { return [... Object.keys(e)] })

i have the keys and values in different maps.

now i would like to get both in one map, so i can loop through it and get keys and values.

i tried some versions of merging the maps but nothing worked.

may be creating a new map out of the two ?

For an example of the wished structure :

From

payload: array[8]
0: object

Feldkennung: 104101
Feldname: "Projektnummer"
WertimFeld: "123 dummy"

too

payload: array[8]
0: object

0:                  104101
1:                  "Projektnummer"
2:                  "123 dummy"
3:                  "Feldkennung"
4:                  "Feldname"
5:                  "WertimFeld"

That seems like an odd thing to want but anyhow, it looks like you just want to join the 2 arrays.

You can easily achieve that with the spread operator...

const werte = msg.payload.map(e=> { return [...Object.values(e)] });
const namen = msg.payload.map(e=> { return [...Object.keys(e)] });
msg.payload = [...werte, ...namen];
return msg;

The result:
mergedmap

i could work with that solution but may be there is a solution that works not like a push?

May be there is a simplier solution for my problem, i try to formulate my problem again.

I use a SQL query. As a result i get some datasets.
These Datasets differ in amount of entrys, names of the entrys and the values.
For every dataset i want to get the sructure key:value for all entrys.

if i get everything in an array with objects like:

0:                  104101
1:                  "Projektnummer"
2:                  "123 dummy"
3:                  "Feldkennung"
4:                  "Feldname"
5:                  "WertimFeld"

i can get everything i want dynamicly and in loops without knowing what kind of objects i get from my sql query...

may be i think too complicated ?

My point was that format is terrible. An object or array of objects is far more suited.

Try these 3 demos - see what suits you...

[
    {
        "id": "eb890c42660627b1",
        "type": "inject",
        "z": "eb3bd3ff.33367",
        "name": "fake DB data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"Feldkennung\":104101,\"Feldname\":\"Projektnummer\",\"WertimFeld\":\"123 dummy\"},{\"Feldkennung\":104101,\"Feldname\":\"Projektnummer\",\"WertimFeld\":\"123 dummy\",\"AnotherFeld\":\"another value\"}]",
        "payloadType": "json",
        "x": 920,
        "y": 660,
        "wires": [
            [
                "7eb4c0d1f00403b5",
                "883b851d53d857a2",
                "33aead0e2507b653"
            ]
        ]
    },
    {
        "id": "7eb4c0d1f00403b5",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "what you are after",
        "func": "const werte = msg.payload.map(e => { return [...Object.values(e), ...Object.keys(e)] });\nmsg.payload = werte.flat(2);\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1150,
        "y": 600,
        "wires": [
            [
                "8e518250ae8f4e2f"
            ]
        ]
    },
    {
        "id": "8e518250ae8f4e2f",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 600,
        "wires": []
    },
    {
        "id": "883b851d53d857a2",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "a better way",
        "func": "const werte = msg.payload.map(e => Object.entries(e));\nmsg.payload = werte.flat(1);\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1130,
        "y": 660,
        "wires": [
            [
                "38e4234858c1392e"
            ]
        ]
    },
    {
        "id": "33aead0e2507b653",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "much better (keeps name+value together)",
        "func": "const data = msg.payload;\nconst result = [];\nlet rowNo = 0;\ndata.forEach(d => {\n    rowNo++;\n    let nvarr = Object.entries(d);\n    nvarr.forEach(e => {\n        const nvpair = {\n            row: rowNo,\n            field: e[0],\n            value: e[1]\n        }\n        result.push(nvpair)\n    })\n})\nmsg.payload = result;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1220,
        "y": 720,
        "wires": [
            [
                "fd54add26e56f888"
            ]
        ]
    },
    {
        "id": "38e4234858c1392e",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 660,
        "wires": []
    },
    {
        "id": "fd54add26e56f888",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 720,
        "wires": []
    }
]

i will try to understand the code and will respond after i tried that.

thanks!

On demo 1 & 2 i get:"werte.flat is not an function". I cant see whats wrong there.
But i learned the method .flat :slight_smile:

Demo 3 works and seems to be the solution.

I will try that solution and will respond after that. May be tomorrow.

Thanks for your time!

You must be on node-js V10 or below. compatibility report

I would definitely recommend upgrading as that is EOL / out of support.

V14 LTS (or even V16) is now preferable.

I needed to use an other option, but i learned something new.

Thanks!

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