How to get JSON from last node?

I make a http request to get a json object. I now like to make another http request to send this object to another endpoint. But I’m not able to get the same data structure into my HTTP request:

Hi!

It should actually work exactly like that. A good page to test that is this one:
https://httpbin.org/anything
That one sends as a response the same data you did send to it. I just tested it (to be sure I am telling you the truth) and it worked fine.

The [Object:...] in the result is not exactly the data which does get send there. It will not send [Object:...] it is just a way to visually show people that it sets an actual JavaScript-Object and not some kind of string, number, boolean. If you want it to display only the data and not the [Object:...] you can simply add a JSON.stringify. Then it will be converted to a string and will be displayed as such.

Sorry is apparently not obvious. Have to add proper documentation about that or find a better way to display it to make it clearer.

1 Like

Ah, got you. Thanks, I will test it right away :slight_smile:

I used https://webhook.site to get the response. But it is not working, this is my response: “[object Object]”

This is the expression I created:

{
  "properties":{},
  "routing_key":"create",
  "payload":"{{$node["HTTP Request"].data}}",
  "payload_encoding":"string"
}

and the output is:

{
  "properties": {},
  "routing_key": "create",
  "payload": "[object Object]",
  "payload_encoding": "string"
}

Got it:

{
  "properties":{},
  "routing_key":"create",
  "payload":"{{JSON.stringify($node["HTTP Requestt"].data ).replace(/"/g, "\\\"")}}",
  "payload_encoding":"string"
}

That does not seem like the correct answer for me.

This one works totally fine like written above without any stringify or replace:

Btw. to send the data you want to send above you would simply do this:

{ 
  "properties": {}, 
  "routing_key": "create", 
  "payload": {{JSON.stringify($node["HTTP Requestt"].data)}}, 
  "payload_encoding": "string"
}

I tested this, it only works for me with the replacement.

{{JSON.stringify($node["REQUEST"].data ).replace(/"/g, "\\\"")}}
{\"id\":\"5d8....1989\",\"date\":\"2019-09-27T14:11:32.8998044+00:00\",\"Number\":\"900..

{{JSON.stringify($node["REQUEST"].data )}}
{"id":"5d8......1989","date":"2019-09-27T14:11:32.8998044+00:00","Number":"900..

Sorry still does not make any sense to me.

Did you really test what I did post above in n8n? Exactly like I did add it? So copied the JSON in n8n so that it creates the nodes and connections and then press “Execute”. Because then it has to work!

Maybe all you did is to remove the replace but still left the quotes around it?

Here again an example with the full data you apparently want to send:

If you do what you described above, you will get this data displayed on webhook.site:

But then the payload has to get JSON.stringified.

What you normally want is this:

Sure it is possible that some endpoint in the Internet expects the data as a string which it has to be JSON.stringified but that is probably not what the most people would want when they see this question. So fear that the answer you did set as solution would cause problems for people that try it as it will not result in “full” JSON. It would be JSON with a string inside which can be parsed to JSON.

I see your solution is working (with your JSON) but with mine I get validation errors:

Error: Parse error on line 4:
...ate",	"payload":"{"id":"5d8e31ea6bd1135
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'undefined'

This is why I escape mine - my endpoint seams to have no problems with the escaped JSON. As a side note, my JSON is also valid if I not need to put it in this structure.

Yes that error makes sense if you still have the quotes around the expression. So you just have to change:

"payload": "{{JSON.stringify($node["HTTP Requestt"].data)}}",

to

"payload": {{JSON.stringify($node["HTTP Requestt"].data)}},
1 Like

Now I got you :boom:

Great, now the worls makes sense for me again :wink:

"payload": "{{JSON.stringify($node['HTTP Requestt'].data)}}",

would work as well, right?

No that would cause the same problems. As the result would be something like this:

{
  "payload" : "{ "the data": "asdf" }"
}

So you have again quotes in quoted text.

1 Like