How to read specific value from Array

#1

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!

0 Likes

#2

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).

0 Likes

#4

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

0 Likes

#5

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;

0 Likes

#6

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.

0 Likes

#7

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.

0 Likes

#8

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

1 Like

#9

Yep, more efficient, great!

0 Likes

#10

Thank you guys, krambriw's solution worked perfectly!

0 Likes