UweG
September 7, 2021, 7:04am
1
Hi there
I try to acces a specific value of a previous node with the function node.
This is the output of the previous node:
So i try this in the function node:
var vJSON = $node["Webhook"].json['body'];
items[0].json.encodeDatabaseID = vJSON;
return items;
So i get this:
How i get following output:
“encodeDatabasID”:{“searchDatabase” : “e8wajdq9msqt”}
Hey @UweG ,
The body object contains a string and not JSON. You will have to first convert it to JSON using the JSON.parse()
method.
This code snippet might help
var vJSON = JSON.parse($node["Webhook"].json['body']);
items[0].json.encodeDatabaseID = vJSON;
return items;
UweG
September 7, 2021, 8:15am
3
Hi harshil
Thank’s for your answer but this does not work
I want only the value of ‘DatabaseID’, which is ‘e8wajdq9msqt’ to assign it to a variable.
Not the whole json body.
Hey @UweG ,
In that case you will have to refer to that value after parsing the body as JSON.
This code might be useful
var vJSON = JSON.parse($node["Webhook"].json['body']);
const db_id = vJSON['DatabaseID'];
items[0].json.encodeDatabaseID = db_id;
return items;
UweG
September 7, 2021, 8:28am
5
With this code i get following error:
Can you share the output of the previous node? I’ll try to replicate it and give you a solution.
UweG
September 7, 2021, 8:56am
7
Hi Harshil.
I have make an Mistake to send data to the Webhook.
Now i got the body as JSON Objekt instead of a string.
The Webhook output looks now like this:
Jon
September 7, 2021, 9:12am
8
@UweG using your new layout in your function you should be able to just use $node["Webhook"].json['body'].DatabaseID;
Using…
curl -X POST https://n8n.mydomain.tld/webhook-test/comm/test -H "Content-Type: application/json" --data '{"TeamID":"xxxx","DatabaseID":"yyyy"}'
It results in my webhook having…
Then as an ugly function I have…
items[0].json.SomeValue = $node["Webhook"].json['body'].DatabaseID;
return items;
Which is returning…
Although depending on what you are doing you can normally select the Node in the expression editor and that will allow to visually select what you are after.
Hopefully this helps
UweG
September 7, 2021, 9:21am
9
Hello Jon
This is it.It works perfect.
How can i use the expression editor in a function node?
#Harshill , sorry for my fault to transfer the body correctly.
Jon
September 7, 2021, 9:22am
10
Ah in the function node it isn’t something you can use but for other nodes it is an option.