How to read specific value from Array

Hi, I'm struggling with a (maybe simple) problem that I cannot solve. I'm reading values from SQL and get the following message:

[ [ { "id": 2, "tekst": "Costam", "czekolada": 1, "imie": "Axxxx", "nazwisko": "Nxxxx", "ulica": "Sxxxx", "dom": "12", "mieszkanie": "35", "kod": "01-348", "miasto": "Warszawa", "telefon": "123456789" } ], "[circular]" ]

Now I need to separate only the value "tekst" and send it to another node. I'm trying to read it via function and separate it via msg.payload[1].tekst but such construction doesn't work. What is more, when I return msg.payload it get (undefined) state and show no values, only return msg; returns the same message as SQL node. Do you have any suggestions how I can read the specific value?

Thanks in advance!

Have you inspected the data in the debug sidebar? If so, you can hover the mouse over any value you see, and get its "path" by clicking the first button on the right (looks a bit like >_) -- so no need to guess how to get at the data... BTW, this post demonstrates this technique, and has more tips on how to access and use msgs in your flows.

The "Copy path" button puts the path to that data relative to the last object shown by the debug node onto the clipboard -- for instance, if this data is from msg.payload, then the copied path would be something like payload[0][0].tekst. Incidentally, the leading double square brackets means that your query is returning an array of arrays of objects (pretty typical of db nodes that support multiple statements -- the outer array holds each result set array).

I do use debug sidebar but don't have such option you described, all information shows up as plain text...

under construction...

I was under the impression that the data you receive is an array. If so you can put this code into a function node

msg.payload = msg.payload[0][0].tekst;
return msg;

If the datatype shown in the debug sidebar is "string" then you can try adding a json node after your db query, to convert the resulting string into a JS object... then if you wire the output to the debug node it should work for you.

Here you can see your data string and the msg object after a json node converts it:

Notice how the datatype is shown at the top, such as "msg.payload: string[220]" -- after it is converted to a object, then you can grab certain properties out of the msg, using the "copy path" button shown in the second output msg.

or just use a change node to "Move" mag.payload[0][0].tekst "to" msg.payload ;*)

2 Likes

Yep, more efficient, great!

Thank you guys, krambriw's solution worked perfectly!


Hy! I am new to node-red and Json. I am having an issue, Could someone please help me in resolving it.
I am using a WIFI scanner node to get info off all the wifi network available. Now i want to read the only signal_level value from object and display it. How is it possible?

Hi,

If you hover the mouse over the 'signal-level' debug display it will pop-up three icons. The left-most icon will give you the full path into the variable - suitable to use with the Change node.

That lets you 'copy' the value into a variable for later use - perhaps in a comparison etc.

Cheers,

Paul

1 Like

Thanks for the help